How to Use Preg_Replace_Callback

How to use preg_replace_callback?

Use the following:

$out = preg_replace_callback(
"(\[otsection\](.*?)\[/otsection\])is",
function($m) {
static $id = 0;
$id++;
return "<div class=\"otsection\" id=\"ots".$id."\">".$m[1]."</div>";
},
$in);

In particular, note that I used a static variable. This variable persists across calls to the function, meaning that it will be incremented every time the function is called, which happens for each match.

Also, note that I prepended ots to the ID. Element IDs should not start with numbers.


For PHP before 5.3:

$out = preg_replace_callback(
"(\[otsection\](.*?)\[/otsection\])is",
create_function('$m','
static $id = 0;
$id++;
return "<div class=\"otsection\" id=\"ots".$id."\">".$m[1]."</div>";
'),
$in);

use preg_replace_callback with array

There are several issues:

  • Use $change_to in the anonymous function, not $array
  • Use regex delimiters around the pattern (e.g. /.../, /\w+/)
  • If there is no such an item in $change_to return the match value, else, it will get removed (the check can be performed with isset($change_to[$match[0]])).

Use

$title = "Tom's wife is called Tomasina";

$change_to = array(
"Tom" => "Fred",
"wife" => "girlfriend",
);

$title = preg_replace_callback('/\w+/', function( $match ) use ( $change_to ) {
return isset($change_to[$match[0]]) ? $change_to[$match[0]] : $match[0];
}, $title);
echo $title;
// => Fred's girlfriend is called Tomasina

See the PHP demo.

Also, if your string can contain any Unicode letters, use '/\w+/u' regex.

How to use preg_replace_callback

Updated

Using built-in counter variable of preg_replace_callback:

$new_text = preg_replace_callback("/$target_str/U", 
function($matches) use (&$count, $replacement_str)
{
$count++;
return ($count == 2) ? $replacement_str : $matches[0];
} , $article_text, -1, $count);

How to use a static method as the callback parameter in preg_replace_callback()?

In PHP when using a class method as a callback, you must use the array form of callback. That is, you create an array the first element of which is the class (if the method is static) or an instance of the class (if not), and the second element is the function to call. E.g.

class A {
public function cb_regular() {}
public static function cb_static() {}
}

$inst = new A;

preg_replace_callback(..., array($inst, 'cb_regular'), ...);

preg_replace_callback(..., array('A', 'cb_static'), ...);

The function you are calling of course has to been visible from within the scope where you are using the callback.

See http://php.net/manual/en/language.pseudo-types.php(dead link), for details of valid callbacks.

N.B. Reading there, it seems since 5.2.3, you can use your method, as long as the callback function is static.

preg_replace_callback just take 5 parameter?

From the PHP manual:

Version 7.4.0 The flags parameter was added.

Upgrade your php version.


Ideally, you shouldn't be using a preg_ function to modify a valid html document. You should use a legitimate dom parser.



Related Topics



Leave a reply



Submit