hungryroy Posted April 13, 2008 Share Posted April 13, 2008 Hi there, I have a PHP script that I use to modify some contents according to tagging rules. My initial code is like this: $content = preg_replace( '/(\[TEST\])(^\[)*(\[\/TEST\])/', '<a href="http://localhost/test.php?param=${2}">${2}</a>', $content); As you can see, I'm trying to transform everything inside [TEST][/TEST] tags into a URL that uses the content of the tags as a parameter. In most cases it works fine, except sometimes when the content contains two [TEST] tags in the same line, i.e. A [TEST]test[/TEST] of two [TEST]lines[/TEST]. In this case, it matches the first opening TEST tag to the second closing /TEST tag, i.e. it interprets it as only one big TEST tag pair instead of two tag pairs. I'm fairly bad at regular expressions, so I could use some help. Any suggestions? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/100887-solved-tag-replacement-regex/ Share on other sites More sharing options...
Daniel0 Posted April 13, 2008 Share Posted April 13, 2008 Try this instead. $content = preg_replace( '/\[TEST\](.*)\[\/TEST\]/U', '$1', $content); Quote Link to comment https://forums.phpfreaks.com/topic/100887-solved-tag-replacement-regex/#findComment-515952 Share on other sites More sharing options...
hungryroy Posted April 13, 2008 Author Share Posted April 13, 2008 It worked! Thanks! What does the /U mean? Quote Link to comment https://forums.phpfreaks.com/topic/100887-solved-tag-replacement-regex/#findComment-515974 Share on other sites More sharing options...
Daniel0 Posted April 13, 2008 Share Posted April 13, 2008 It inverts the greediness. * is greedy, so by giving the U modifier it becomes ungreedy. An ungreedy quantifier stops looking as soon as something matches whereas a greedy quantifier keeps on looking until it cannot find any more which matches. Quote Link to comment https://forums.phpfreaks.com/topic/100887-solved-tag-replacement-regex/#findComment-515977 Share on other sites More sharing options...
hungryroy Posted April 13, 2008 Author Share Posted April 13, 2008 Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/100887-solved-tag-replacement-regex/#findComment-515978 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.