Jump to content

Regex (i Think) Help


Chris92
 Share

Recommended Posts

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!

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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 :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...