The Little Guy Posted June 9, 2012 Share Posted June 9, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263898-preg_replace-removing-1/ Share on other sites More sharing options...
requinix Posted June 9, 2012 Share Posted June 9, 2012 preg_replace() isn't doing it. Have you checked what $template is before this tag replacing runs? What are the various $keys and $values? Quote Link to comment https://forums.phpfreaks.com/topic/263898-preg_replace-removing-1/#findComment-1352408 Share on other sites More sharing options...
.josh Posted June 9, 2012 Share Posted June 9, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263898-preg_replace-removing-1/#findComment-1352436 Share on other sites More sharing options...
The Little Guy Posted June 9, 2012 Author Share Posted June 9, 2012 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?!?! Quote Link to comment https://forums.phpfreaks.com/topic/263898-preg_replace-removing-1/#findComment-1352445 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.