w3bariak
w3bariak

Reputation: 25

Replace preg_replace to preg_replace_callback

Can someone please help me with converting code

$file = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $file);

to use preg_replace_callback instead of it ?
Thanks

Upvotes: 1

Views: 526

Answers (2)

CrayonViolent
CrayonViolent

Reputation: 32532

$file = preg_replace_callback('~&#([0-9]+);~', function($m) { return chr($m[1]); }, $file);

If you are on a version of php that doesn't support anonymous functions, you can do this instead:

function myfunction($m) { return chr($m[1]); }
$file = preg_replace_callback('~&#([0-9]+);~', 'myfunction', $file);

Upvotes: 2

Arsen Ibragimov
Arsen Ibragimov

Reputation: 435

It might be something like this:

$file = preg_replace_callback(
           '~&#([0-9]+);~',
           function ($matches) {
              // action
           },
           $file
);

PS. Without e modificator.

Upvotes: 0

Related Questions