Jump to content

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 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 :)

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

 

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.