Jump to content

preg_replace to preg_replace_callback


Go to solution Solved by requinix,

Recommended Posts

can someone help me change this to preg_replace_callback. i'm receiving the following error Warning: preg_replace_callback(): Requires argument 2, '@constant("\1")', to be a valid callback in

//load current language
        foreach ($langs as $key => $value) if ($value['l_code'] == lang) break;
        $f = fopen('languages/'.$langs[$key]['l_file'],'r') or die('Cannot open language file '.$langs[$key]['l_file']);
        while (!feof($f)) {
                $s = chop(fgets($f,4096));
                if ($s{0} == '#') continue;
                list($key,$value) = explode('=',$s,2);
                $value = preg_replace('/<%(.*)%>/sUe', '@constant("\\1")', $value);
                $def[$key] = $value;
                }
        fclose($f);
Link to comment
https://forums.phpfreaks.com/topic/298016-preg_replace-to-preg_replace_callback/
Share on other sites

  • Solution

After you change the function to be preg_replace_callback, you also have to change the second argument to be a function and not a mere string. The simplest way is to use an anonymous function like

function($matches) {
	// ...
}
$matches will be an array just like how preg_match() works, what with [0] being the full string and [1] being a captured group. Inside this function you call constant() and return whatever you want the replacement to be.

 

Give that a shot. If you still have problems, post the code you tried.

Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.