Ravani Posted November 18, 2010 Share Posted November 18, 2010 I'm trying to use preg_replace but I can't get it working. function replaceContent($sContent) { $aReplaceTags = array( "'\[MENU-CARD\](.*?)\[/MENU-CARD\]'is" => '\\1', "'\[CONTACT-FORM\]'is" => "Contactform", ); return preg_replace(array_keys($aReplaceTags), array_values($aReplaceTags), $sContent); } This works great however when I change '\\1' into a function the output of '\\1' will always be 1. function replaceContent($sContent) { $aReplaceTags = array( "'\[MENU-CARD\](.*?)\[/MENU-CARD\]'is" => $this->loadMenuCard('\\1'), "'\[CONTACT-FORM\]'is" => "Contactform", ); return preg_replace(array_keys($aReplaceTags), array_values($aReplaceTags), $sContent); } Anyone knows what I am doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/ Share on other sites More sharing options...
Adam Posted November 18, 2010 Share Posted November 18, 2010 You need to use the 'e' modifier: preg_replace('/.../e' (...) Take a look at example 4 on the manual. Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136163 Share on other sites More sharing options...
ManiacDan Posted November 18, 2010 Share Posted November 18, 2010 Or simply preg_replace_callback Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136171 Share on other sites More sharing options...
Adam Posted November 18, 2010 Share Posted November 18, 2010 I was about to suggest preg_replace_callback(), but he's passing arrays. Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136188 Share on other sites More sharing options...
Ravani Posted November 19, 2010 Author Share Posted November 19, 2010 Thanks for the replies, I'm confused now. I tried the example: $aReplaceTags = array( "/\[MENU-CARD\](\w+)\[\/MENU-CARD\]/e" => $this->loadMenuCard('\\1'.strtoupper('\\2').'\\3'), "'\[CONTACT-FORM\]'is" => "Contactformulier", ); I still get the same result. If I output this value '\\1'.strtoupper('\\2').'\\3' outside of the function it gives me 2 which is correct however when I use the loadMenuCard function it gives me '\1\2\3' as output. Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136535 Share on other sites More sharing options...
Adam Posted November 19, 2010 Share Posted November 19, 2010 Wait why are you using double backslash? That'll escape the first so you're using the literal "\1". Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136537 Share on other sites More sharing options...
Ravani Posted November 19, 2010 Author Share Posted November 19, 2010 Wait why are you using double backslash? That'll escape the first so you're using the literal "\1". I saw it in an example. However I changed it into one backslash, it works for a simple output: "/\[MENU-CARD\](\w+)\[\/MENU-CARD\]/e" => '\1', This gives 2 as output but the output inside the function will be \1 again when using this: "/\[MENU-CARD\](\w+)\[\/MENU-CARD\]/e" => $this->loadMenuCard('\1'), When I output the $aReplaceTags array I get: Array ( [/\[MENU-CARD\](\w+)\[\/MENU-CARD\]/e] => \1 ['\[CONTACT-FORM\]'is] => Contactformulier ) That \1 should be 2 though. Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136541 Share on other sites More sharing options...
Adam Posted November 19, 2010 Share Posted November 19, 2010 Reason you're having problems is because the 'e' modifier struggles with object callbacks (which is presumably why preg_replace_callback exists). Personally I'd loop through the array and replace them one at a time, but using preg_replace_callback(). Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136548 Share on other sites More sharing options...
Ravani Posted November 19, 2010 Author Share Posted November 19, 2010 @MrAdam, I'm using your advice now however I'm getting an error. This is what I have now function replaceContent($sContent) { $aReplaceTags = array( "'\[MENU-CARD\](.*?)\[/MENU-CARD\]'is", "'\[CONTACT-FORM\]'is", ); $oLoadMenu = '$this->loadMenuCard'; //$oContactForm = 'process'; foreach($aReplaceTags as $aTag){ if($aTag == "'\[MENU-CARD\](.*?)\[/MENU-CARD\]'is"){ $sContent = preg_replace_callback($aTag, $oLoadMenu, $sContent); } } return $sContent; } And this is what I'm getting: Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, '$this->loadMenuCard', to be a valid callback in C:\wamp\www\SamandCMS\application\views\helpers\ReplaceContent.php on line 39 Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136572 Share on other sites More sharing options...
Ravani Posted November 19, 2010 Author Share Posted November 19, 2010 Ok I solved the problem by putting loadMenuCard inside the replaceContent function. This way I can call the function without using $this-> However I'm not sure if this is a good solution though. If hope one of you guys know a better solution Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136575 Share on other sites More sharing options...
Adam Posted November 19, 2010 Share Posted November 19, 2010 Take a look at how callbacks work. Out of interest, php.net is running awful slow for me. Is it you? Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136577 Share on other sites More sharing options...
Ravani Posted November 19, 2010 Author Share Posted November 19, 2010 Take a look at how callbacks work. Out of interest, php.net is running awful slow for me. Is it you? Yeah I can't get to PHP.net at all. Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136580 Share on other sites More sharing options...
salathe Posted November 19, 2010 Share Posted November 19, 2010 Yeah I can't get to PHP.net at all. Use one of the many mirrors, e.g. http://docs.php.net/callback Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136582 Share on other sites More sharing options...
Ravani Posted November 19, 2010 Author Share Posted November 19, 2010 Ok thanks, I read the documentation and came to the conclusion that this is the easiest way for me to use it: function replaceContent($sContent) { $sContent = preg_replace_callback("'\[MENU-CARD\](.*?)\[/MENU-CARD\]'is", array($this,'loadMenuCard'), $sContent); $sContent = preg_replace_callback("'\[CONTACT-FORM\]'is", array($this,'loadContactForm'), $sContent); return $sContent; } Quote Link to comment https://forums.phpfreaks.com/topic/219077-preg_replace-replace-text-with-a-function/#findComment-1136598 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.