Chris92 0 Posted August 12, 2009 Report Share Posted August 12, 2009 Im not good with regex and want to replace "{beginsubtitle}Example Subtitle{endsubtitle}" with "Example Subtitle". I found this useful code somewhere but it doesnt work if the i use 2 consecutive "{beginsubtitle}Example Subtitle{endsubtitle}"'s (e.g. "{beginsubtitle}Example Subtitle 1{endsubtitle} {beginsubtitle}Example Subtitle 2{endsubtitle}") <?php$regex = "#([{]beginsubtitle[}])(.*)([{]endsubtitle[}])#e";$output = preg_replace($regex,"('$2')",$output);?> If anyone can help it would be much apriciated! Quote Link to post Share on other sites
Ingolme 1,020 Posted August 12, 2009 Report Share Posted August 12, 2009 Im not good with regex and want to replace "{beginsubtitle}Example Subtitle{endsubtitle}" with "Example Subtitle". I found this useful code somewhere but it doesnt work if the i use 2 consecutive "{beginsubtitle}Example Subtitle{endsubtitle}"'s (e.g. "{beginsubtitle}Example Subtitle 1{endsubtitle} {beginsubtitle}Example Subtitle 2{endsubtitle}")<?php$regex = "#([{]beginsubtitle[}])(.*)([{]endsubtitle[}])#e";$output = preg_replace($regex,"('$2')",$output);?> If anyone can help it would be much apriciated! You need to perform what's called a "lazy search". The * modifier does a "greedy search": it will take everything it can until the last appearance of next limit. To make a search lazy, add a '?' after the modifier.You also have a lot of unnecessary brackets in your expression.I've never see the 'e' modifier before. What does it do?$regex = "#\{beginsubtitle\}(.*?)\{endsubtitle\}#e";$output = preg_replace($regex,"('$1')",$output); Quote Link to post Share on other sites
Chris92 0 Posted August 12, 2009 Author Report Share Posted August 12, 2009 You need to perform what's called a "lazy search". The * modifier does a "greedy search": it will take everything it can until the last appearance of next limit. To make a search lazy, add a '?' after the modifier.$regex = "#\{beginsubtitle\}(.*?)\{endsubtitle\}#e";$output = preg_replace($regex,"('$1')",$output); Thanks that works great :)Found it on google and changed it around a bit so a lot of brackets . I i did try to found out what the 'e' was but couldn't find anything, but it doesn't work witouht it.Thanks again Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 12, 2009 Report Share Posted August 12, 2009 And you're not even curious? The information is freely available.http://www.php.net/manual/en/reference.pcr...n.modifiers.php Quote Link to post Share on other sites
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.