Php 如何从给定字符串中提取数据url? $string='';
我想在PHP中提取数据url参数。使用正则表达式可以执行以下操作:Php 如何从给定字符串中提取数据url? $string='';,php,regex,Php,Regex,我想在PHP中提取数据url参数。使用正则表达式可以执行以下操作: $string = '<img alt="Peachtree St Apartment 2" data-url="http://i.oodleimg.com/item/3472578127t_1m_condos,_townhouses_&_apts_for_sale_in_johnson_city_tn/?1377909664" src="http://i.oodleimg.com/a/animate/
$string = '<img alt="Peachtree St Apartment 2" data-url="http://i.oodleimg.com/item/3472578127t_1m_condos,_townhouses_&_apts_for_sale_in_johnson_city_tn/?1377909664" src="http://i.oodleimg.com/a/animate/spinner-small.gif" class="lazyload-image">';
$string='';
$matches=array();
$url='';
if(preg_match('/data url=“([^”]+)“/”,$string,$matches)){
$url=$matches[1];
}
使用正则表达式可以执行以下操作:
$string = '<img alt="Peachtree St Apartment 2" data-url="http://i.oodleimg.com/item/3472578127t_1m_condos,_townhouses_&_apts_for_sale_in_johnson_city_tn/?1377909664" src="http://i.oodleimg.com/a/animate/spinner-small.gif" class="lazyload-image">';
$string='';
$matches=array();
$url='';
if(preg_match('/data url=“([^”]+)“/”,$string,$matches)){
$url=$matches[1];
}
这对于XPath来说是一项伟大的工作!
不知道XPath是什么?让维基百科:
XML路径语言XPath是一种用于选择节点的查询语言
来自XML文档。此外,XPath还可用于计算值
(例如,字符串、数字或布尔值)从XML的内容中提取
文件。XPath是由万维网联盟(W3C)定义的
因此,对于您的特定用户案例,查询将是:
$string = '<img alt="Peachtree St Apartment 2" data-url="http://i.oodleimg.com/item/3472578127t_1m_condos,_townhouses_&_apts_for_sale_in_johnson_city_tn/?1377909664" src="http://i.oodleimg.com/a/animate/spinner-small.gif" class="lazyload-image">';
$matches = array();
$url = '';
if (preg_match('/data-url="([^"]+)"/', $string, $matches)) {
$url = $matches[1];
}
因此,您可以像这样使用它:
//img[@class='lazyload-image']/@data-url
然后您可以自由地对其进行迭代。在这件事上,我已经考虑了附加到img标签上的类,但请随意删除:
$query("//img[@class='lazyload-image']/@data-url");
$xpath->query($query);
这对于XPath来说是一项伟大的工作!
不知道XPath是什么?让维基百科:
XML路径语言XPath是一种用于选择节点的查询语言
来自XML文档。此外,XPath还可用于计算值
(例如,字符串、数字或布尔值)从XML的内容中提取
文件。XPath是由万维网联盟(W3C)定义的
因此,对于您的特定用户案例,查询将是:
$string = '<img alt="Peachtree St Apartment 2" data-url="http://i.oodleimg.com/item/3472578127t_1m_condos,_townhouses_&_apts_for_sale_in_johnson_city_tn/?1377909664" src="http://i.oodleimg.com/a/animate/spinner-small.gif" class="lazyload-image">';
$matches = array();
$url = '';
if (preg_match('/data-url="([^"]+)"/', $string, $matches)) {
$url = $matches[1];
}
因此,您可以像这样使用它:
//img[@class='lazyload-image']/@data-url
然后您可以自由地对其进行迭代。在这件事上,我已经考虑了附加到img标签上的类,但请随意删除:
$query("//img[@class='lazyload-image']/@data-url");
$xpath->query($query);
您的字符串是空的。您的字符串是空的。这绝对是最好的做法。这里的解释很好+1这里的最佳实践。零度以下,这里的解释很好+你真是太好了。谢谢:)你真是太好了。谢谢:)