Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/169041-matching-text-in-the-middle-of-a-string/
Share on other sites

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]

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.