shardsofmetal Posted August 6, 2009 Share Posted August 6, 2009 I am writing a script that takes data from different web pages and returns part of it. I would like a regex that matches text that starts with the text "<lyrics>", then matches all text that isn't the specific phrase "</lyrics>", and then matches the phrase </lyrics>. Is this possible. It seems to me that it shouldn't be too hard, but I can't figure it out. I'm trying to use preg_match. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/169041-matching-text-in-the-middle-of-a-string/ Share on other sites More sharing options...
thebadbad Posted August 6, 2009 Share Posted August 6, 2009 preg_match('~<lyrics>.*?</lyrics>~s', $source, $matches); The dot matches any character, the asterisk makes it match that zero or more times and the question mark makes the match greedy, so it will stop at the last occurrence of </lyrics>, including possible nested lyrics tags. The s modifier after the delimiter (~) makes the dot match line breaks too. The match is stored in $matches[0] Quote Link to comment https://forums.phpfreaks.com/topic/169041-matching-text-in-the-middle-of-a-string/#findComment-891937 Share on other sites More sharing options...
Adam Posted August 6, 2009 Share Posted August 6, 2009 Yeah, and you can add brackets if you'd like to match just the content of the lyrics tags: preg_match('~<lyrics>(.*?)</lyrics>~s', $source, $matches); If you'd prefer to do it that way, $matches[1] would contain the value. Quote Link to comment https://forums.phpfreaks.com/topic/169041-matching-text-in-the-middle-of-a-string/#findComment-891976 Share on other sites More sharing options...
nrg_alpha Posted August 6, 2009 Share Posted August 6, 2009 preg_match('~<lyrics>.*?</lyrics>~s', $source, $matches); ...and the question mark makes the match greedy, so it will stop at the last occurrence of </lyrics>, including possible nested lyrics tags. The s modifier after the delimiter (~) makes the dot match line breaks too. The match is stored in $matches[0] I think you mean to say that the question mark makes it lazy, and will stop at the first occurrence of </lyrics> (regardless of nesting or not) you would have to eliminate the ? metacharacter to make your statement true. Quote Link to comment https://forums.phpfreaks.com/topic/169041-matching-text-in-the-middle-of-a-string/#findComment-892186 Share on other sites More sharing options...
thebadbad Posted August 6, 2009 Share Posted August 6, 2009 Thanks NRG, I stand corrected I still for some reason mix up greediness and laziness sometimes o_0 Quote Link to comment https://forums.phpfreaks.com/topic/169041-matching-text-in-the-middle-of-a-string/#findComment-892416 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.