I am trying to get HTML image tag url from the given string. I am using the PHP method preg_match("/src=\"([^\s\"]+)/",$body,$matches);
which works fine for most cases, however I am having trouble getting the url from the following link...
src=http://4.bp.blogspot.com/-LqmY1y4DCO0/T39EoCmzfoI/AAAAAAAACp0/TRKmdPmhJ90/s640/how+to+make+cheesecake+carrots,+orange+cheesecake+recipe,+recipes,+Easter+dessert+.jpg
Could anyone explain the reason for this failing please.
Your test string does not have " character after src= and your regular expression expects it so it does not match. If you want to test your regular expressions I recommend you use program RegexCoach, it highlights matches and allows you to step through matching processs so you can easily see where it fails.
loading with DOM?
$dom = new DOMDocument();
$dom->loadHTML($var);
$xpath = new DOMXPath($dom);
$img = $xpath->query('//img[@src]');
if(count($img)>0)
{
foreach($img as $i)
echo $i->getAttribute('src');
}
try it with this regex :)
preg_match_all("/src=([^\\s]+)/", $body, $m);
print_r($a);