Jump to content

preg_replace, replace text with a function


Ravani

Recommended Posts

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?

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. :(

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.

 

 

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().

@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

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;
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.