hello im just having a problem here with PREG_MATCH_ALL() lets say i have 2 strings ... Save You and What If Uploader Name i need to retreive Save You so i'm using the folowing paterns
preg_match_all('/<span id="title.*">(.*)<\/span>/i',,);
as a result i get
Save You Uploader Name
I think this is because it's taking the last any way to fix it ? or like matching the first ? thank you
EDIT:
<a href="#" onclick="searchActions.showLyrics('14840_100733666_21',9965243); return false;">Can't Keep My Hands Off You</a></span>
get as result
<a href="Your Love Is A Lie"</a>
with
/<span id="title(.*?)">(.*?)<\/span>/i
The reason is that .*
is greedy. The first .*
(right after title
) will swallow as many characters as it can, and then backtrack from the end of the string until the rest of the regexp can match.
To fix it, either use the ungreedy version .*?
, and/or replace the .
with some character class that can't match a quote (such as [^"]
):
preg_match_all('/<span id="title[^"]*">(.*?)<\/span>/i', ..., ...);
For more information, see http://www.php.net/manual/en/regexp.reference.repetition.php
(Ps. Levi Morrison is right — this is not a good way to parse HTML. Use a real HTML parser, such as DOMDocument::loadHTML()
.)