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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.