...

View Full Version : Extracting a href and title out of this string



kevinkhan
10-27-2009, 03:23 PM
Hi Guys,

I want to get the href and title out of this html code below..


<li class="images"><a href="www.something.com/firstpage/" title="title1"><span>7</span></a></li>

I set the html code as a string



$strContent= "<li class="images"><a href="www.something.com/firstpage/" title="title1"><span>7</span></a></li>";

and set up this funtion with the regular expression below




$strListMatches = '!<li class="images" href="(.*)" title="(.*)"><span>(.*)</span</a></li>!isU';


function getMatches($strMatch,$strContent)
{
if(preg_match_all($strMatch,$strContent,$objMatches))
{
return $objMatches;
}
return "";
}

i tryed this piece of code to print the title but it wont work.


print_r($objListMatches[1]);

Is there something wrong with the regular expression?



iv tryed this regular expression but it wont work :(


$strListMatches = '!<li class="images" href="(.*)" title="(.*)"><span>(.*)</span></a></li>!isU';

Lamped
10-27-2009, 03:26 PM
Don't use .* unless you fully understand it. If you want to extract a quoted section, you should use ".*?" or "[^"]*"

Phil Jackson
10-27-2009, 03:45 PM
#<[^>]*href=\"([^\"]*)\"[^>]*>#is



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum