Jump to content
Sign in to follow this  
Chris92

Regex (i Think) Help

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!

Share this post


Link to post
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);

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...