indigo diingo Posted March 12, 2009 Share Posted March 12, 2009 Hello I've found a simple regex to replace the string beginning with '<span id="unique id">some text</span><span id="another unique id">some text</span><span id="another unique id">some text</span>' with 'hello'. $original = preg_replace ("/<span id=\"$id\">(.*)<\/span>/",'hello',$string); The problem now is that the regex stops at the last '</span>' tag so my hole string get replaced by 'hello' but I only want to replace the string from the beginning '<span>' tag with unique id to the FIRST end '</span>' tag, not the last. How can I solve this? Thanks, Indigo Quote Link to comment https://forums.phpfreaks.com/topic/149113-regex-doesnt-stop-when-it-needs-to/ Share on other sites More sharing options...
sasa Posted March 12, 2009 Share Posted March 12, 2009 try $original = preg_replace ("/<span id=\"$id\">.*?<\/span>/",'hello',$string); Quote Link to comment https://forums.phpfreaks.com/topic/149113-regex-doesnt-stop-when-it-needs-to/#findComment-783109 Share on other sites More sharing options...
effigy Posted March 12, 2009 Share Posted March 12, 2009 You have a laziness/greediness issue. Quote Link to comment https://forums.phpfreaks.com/topic/149113-regex-doesnt-stop-when-it-needs-to/#findComment-783161 Share on other sites More sharing options...
indigo diingo Posted March 12, 2009 Author Share Posted March 12, 2009 You have a laziness/greediness issue. Well i would not call it lazy when i've been searching for hours to find the answer and didn't. Thanks to the other person who responded and didn't call my issue lazy. Quote Link to comment https://forums.phpfreaks.com/topic/149113-regex-doesnt-stop-when-it-needs-to/#findComment-783171 Share on other sites More sharing options...
severndigital Posted March 16, 2009 Share Posted March 16, 2009 lol .. your issue isn't lazy .. you search is there is a parameter for doing a lazy search or a greedy search. Quote Link to comment https://forums.phpfreaks.com/topic/149113-regex-doesnt-stop-when-it-needs-to/#findComment-785762 Share on other sites More sharing options...
Daniel0 Posted March 16, 2009 Share Posted March 16, 2009 You have a laziness/greediness issue. Well i would not call it lazy when i've been searching for hours to find the answer and didn't. Thanks to the other person who responded and didn't call my issue lazy. Laziness and greediness are two terms used when talking about regular expressions. If a search is greedy then it keeps on looking for matches as long as it can. It's wants as much as possible, it's greedy. A lazy search, however, stops as soon as it finds something that matches. It's lazy, it does the absolute minimum it has to. It's just two metaphors you use in regex. fenway wasn't talking about you being neither lazy nor greedy. Quote Link to comment https://forums.phpfreaks.com/topic/149113-regex-doesnt-stop-when-it-needs-to/#findComment-785879 Share on other sites More sharing options...
.josh Posted March 16, 2009 Share Posted March 16, 2009 You have a laziness/greediness issue. Well i would not call it lazy when i've been searching for hours to find the answer and didn't. Thanks to the other person who responded and didn't call my issue lazy. haha doh! Quote Link to comment https://forums.phpfreaks.com/topic/149113-regex-doesnt-stop-when-it-needs-to/#findComment-785882 Share on other sites More sharing options...
xylex Posted March 16, 2009 Share Posted March 16, 2009 You have a laziness/greediness issue. Well i would not call it lazy when i've been searching for hours to find the answer and didn't. Thanks to the other person who responded and didn't call my issue lazy. LMAO That one was great. Quote Link to comment https://forums.phpfreaks.com/topic/149113-regex-doesnt-stop-when-it-needs-to/#findComment-786038 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.