我怎么能采取src值从jquery缩放使用正则表达式在php



我试过了

$content =file_get_contents("http://www.starplugins.com/cloudzoom");
preg_match('/<img[sS][^>]*src="([^"]*)"[^>]*id="zoom1"[^>]*>/',$content,$arr);
print_r($arr);

返回空数组,但是当只取html部分并使用Rubular进行测试时它给出了正确的输出。请帮忙解决

try this:

/<img.*?id=["']zoom1["'].*?src=["'](.+?)["'][s]{0,}.*?/{0,1}>/ims

如果返回false,他们尝试如下:

/<img.*?src=["'](.+?)["'][s]{0,}.*?id=["']zoom1["'].*?/{0,1}>/ims

.

基本上,正则表达式将高度依赖于id和src的顺序。

但是,由于这里处理的是html,所以最好使用DOM/XPath。

你应该使用DOM:

$dom = new DOMDocument();
@$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$src = $xpath->query('//img[@id="zoom1"]')->item(0)->getAttribute('src');

真的容易多了,正确的使用方法=)

最新更新