我正在尝试从外部html页面中获取一个值。
这对我来说很神奇:
preg_match_all('/id="localWeather">(.*?)<\/div>/',$returnedPage,$returnValues,PREG_SET_ORDER);但是在这行之后,我需要循环结果并清理它。
为什么?因为我只需要这个(.*?),而且由于某种原因,它还会返回,所以我还需要添加额外的</div>结束标记,所以我需要循环它,然后清理数组。
我的问题是如何强制只返回以下内容:(.*?)?
发布于 2015-01-22 18:25:15
摆脱PREG_SET_ORDER。示例:
<?php
$returnedPage = '<div id="localWeather">test</div><div id="localWeather">test2</div>';
preg_match_all('/id="localWeather">(.*?)<\/div>/',$returnedPage,$returnValues);
print_r($returnValues);输出:
Array
(
[0] => Array
(
[0] => id="localWeather">test</div>
[1] => id="localWeather">test2</div>
)
[1] => Array
(
[0] => test
[1] => test2
)
)因此,在本例中,$returnValues[1]是一个匹配数组,只包含div之间的内容(而不是结束div),而$returnValues[0]是与regex匹配的字符串的整个部分的数组。
此外,不建议使用正则表达式来解析HTML。我看一下PHP的DOMDocument类,它更健壮。
https://stackoverflow.com/questions/28095982
复制相似问题