Jump to content

preg_replace removing $1


The Little Guy

Recommended Posts

I am not sure what I am doing wrong, but I am doing this to replace values within a template:

 

$template = preg_replace("/\<!--\[$key\]--\>/sU", $value, $template);

 

foreach($this->items as $key => $value){
$key = preg_quote($key, "/");
if(is_string($key) && (is_string($value) || is_int($value) || is_numeric($value) || is_integer($value))){
	$template = preg_replace("/\<!--\[$key\]--\>/sU", $value, $template);
}
}

 

basically it loops through the array and replaces everything in the template.

 

I was then looking through my site and saw this:

    $replace = array( 
        '<b></b>', 
        '<i></i>', 
        '<span style="text-decoration:underline;"></span>', 
        '<span style="font-size:px;"></span>', 
        '<span style="color:;"></span>' 
    ); 

 

all the $1 and $2 are missing between the tags, and in the attributes, but as you can see they are still in the variables. The last thing after that foreach is an echo statement that echo's out the template and that is the very last thing to happen on the page.

 

So why is my preg_replace removing those? I echo out the key right before it replaces and they are in there, but as soon as the replace happens, they are gone...

 

What is causing this?

 

Example: http://phpsnips.com/snip-41

Link to comment
Share on other sites

1) Your regex does not have any captured groups so there is not $1 or $2

2) your preg_replace doesnt't use that $replace

 

Are you sure you are even looking at the right code?

 

3) you should know the song and dance by now: post examples of what the regex is supposed to be looking for and what you expect the outcome to be

Link to comment
Share on other sites

preg_replace() isn't doing it. Have you checked what $template is before this tag replacing runs? What are the various $keys and $values?

actually it is, because when I woke up this morning I though why use preg_replace try str_replace instead:

 

$template = str_replace("<!--[$key]-->", $value, $template);

 

It works perfect! So It looks like a good night sleep may have solved the issue?!?!

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.