LemonInflux Posted April 24, 2008 Share Posted April 24, 2008 I have my regex function here: preg_replace("#\{% $tag %\}#si", $replace, $str) If that could be tidier, please tell me. Basically, what it does, is go through $str, find matches for: {% TAG_NAME_HERE %} and replace that all with $replace. For example, if $tag = bob and $replace = jim, this string: 'Hello, {% bob %}' would be changed to: 'Hello, jim' Now, what I want to do is add something so if I do, say... 'Hello {{% bob %}}' that doesn't change it, because it has a double brackets. In a nutshell, what I'm asking is... How do I change "#\{% $tag %\}#si" to only pick up {% tag %} and not {{% tag %}} Sorry if this is terribly explained. Just ask and I'll try to explain it a bit more simply. Thanks in advance! Tom. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 24, 2008 Share Posted April 24, 2008 #(?<!\{)\{% $tag %\}(?!\{)#si #(?<!\{)\{% $tag %\}(?!\})#si If $tag is coming from an outside source, be sure to pass it through preg_quote. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted April 24, 2008 Author Share Posted April 24, 2008 Thanks! Can't test it just yet, but if there's any problems I'll post again. Many thanks! Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted April 25, 2008 Author Share Posted April 25, 2008 Haven't checked yet, but should it be.... [pre]#(?<!\{)\{% $tag %\}(?!\})#si[/pre] Instead? Looking at the last bit on yours: (?!\{)#si, surely because the brace is facing right, that would mean {{TAG}{ would work, not {{% TAG %}}? Quote Link to comment Share on other sites More sharing options...
effigy Posted April 25, 2008 Share Posted April 25, 2008 Right. My oversight. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted April 25, 2008 Author Share Posted April 25, 2008 Ok, thanks Quote Link to comment 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.