例 6.1. 从html中提取skydrive图片的url
想要从一段html中提取出这样的地址:
http://storage.live.com/items/9A8B8BF501A38A36!1211?filename=%e7%82%b9%e5%87%bb%e7%ac%ac%e5%87%a0%e9%a1%b5.jpg
然后对应的php代码为:
# current support valid pic suffiex: 'bmp', 'gif', 'jpeg', 'jpg', 'jpe', 'png', 'tiff', 'tif'
$picSufChars = "befgijmnptBEFGIJMNPT";
//$pattern = '#<a href="(?P<picUrl>http://storage.live.com/items/.+?filename=.+?\.jpg)"><img.+?src="(?P=picUrl)".+?/></a>#';
$pattern = '#<a href="(?P<picUrl>http://storage.live.com/items/.+?filename=.+?\.['.$picSufChars.']{3,4})"><img.+?src="(?P=picUrl)".+?/></a>#';
if(preg_match($pattern, $content, $matches)){
$picUrl = $matches["picUrl"];
//print "found picUrl=".$picUrl;
if(preg_match('#http://storage\.live\.com/items/.+?filename=(?P<filename>.+)#', $picUrl, $foundFilename)){
$filename = $foundFilename["filename"];
$filename = rawurldecode($filename);
}
else{
$filename = "";
}
} else {
$picUrl = "";
}