grethe Posted May 23, 2007 Share Posted May 23, 2007 Hi i need help on the function preg_replace_callback(). I always get this error: Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'make_br', to be a valid callback Here is my code: $text = preg_replace_callback('/(\<p\>)(.+)(\<\/p\>)/sU', 'make_br', $text); function make_br($matches) { $matches['2'] = trim($matches['2']); $matches['2'] = preg_replace('/\n/', "<br/>\n", $matches['2']); return $matches['2']; } What am i doing wrong? Quote Link to comment Share on other sites More sharing options...
effigy Posted May 23, 2007 Share Posted May 23, 2007 I'm not having any trouble with it. What version of PHP are you using? Does it work if you isolate it from surrounding code? Quote Link to comment Share on other sites More sharing options...
grethe Posted May 23, 2007 Author Share Posted May 23, 2007 Yes you're right it works fine if i isolate it. Thanks. I've also figured out how to make i work, but there's something in PHP i don't understand then. Can't a function be created anywhere - like in a function and stille work everywhere? This works: <?php $text = "123"; echo interpret($text); function interpret($text) { $text = preg_replace_callback('/(\<p\>)(.+)(\<\/p\>)/sU', 'make_br', $text); function make_br($matches) { $matches['2'] = trim($matches['2']); $matches['2'] = preg_replace('/\n/', "<br/>\n", $matches['2']); return $matches['2']; } return $text; } ?> This doesn't work: <?php $text = "123"; echo interpret($text); function interpret($text) { $text = preg_replace_callback('/(\<p\>)(.+)(\<\/p\>)/sU', 'make_br', $text); return $text; } function make_br($matches) { $matches['2'] = trim($matches['2']); $matches['2'] = preg_replace('/\n/', "<br/>\n", $matches['2']); return $matches['2']; } ?> Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 23, 2007 Share Posted May 23, 2007 It was the first one that didn't work for me. The second one did. However, the first one did work when I moved the inner function definintion above the preg_replace_callback that called it. PHP functions are supposed to be able to be referenced before their definition but maybe that doesn't apply to functions defined within functions. Quote Link to comment Share on other sites More sharing options...
grethe Posted May 23, 2007 Author Share Posted May 23, 2007 Whoops.. yes you are right. I mixed them. Hmm.. but still that explanation i think is weird, why shouldn't they work like everywhere else. But maybe you are right. Does annyone know why the function behaves like this? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 23, 2007 Share Posted May 23, 2007 The inner function isn't defined until the outer function is executed. Also see: http://us.php.net/manual/en/language.functions.php#21149 Quote Link to comment Share on other sites More sharing options...
grethe Posted May 23, 2007 Author Share Posted May 23, 2007 Thanks a lot.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.