我想从网站源代码中获取一些数据。我想要做的是获得/collections/(whatever that follows here)
之后的所有内容。我的模式与我所寻找的“最”相匹配。当我的preg_match_all
达到“&”的模式时,就会发生问题,此时它将简单读到“&”的位置并停止读取其余部分。这里是我的脚本:PHP preg_match_all不正确匹配
$homepage = file_get_contents('http://www.harrisfarm.com.au/');
$pattern = '/collections([\w-&\/]*)/i';
preg_match_all($pattern, $processedHomePage, $collections);
print_r($collections);
注意,这样打印时,事物的“&”被忽略之后,这意味着它会得到我:
/collections/seafood/Shellfish-&
但是,当我的模式匹配上一个串如下这样:
$subject = 'a href="/collections/organic/Pantry/sickmonster/grandma" <a href="/collections/seafood/Shellfish-&-Crustaceans">Oysters, Shellfish & Crustaceans';
能把我我想要的一切:
/collections/seafood/Shellfish-&-Crustaceans
所以我想......为什么会发生这种情况?我真的很难过。
好像[它应该匹配(http://regex101.com/r/ tY7sE1/1)?你确定'&'没有被转义到'&',因为你正在处理网页? – 2014-11-24 22:12:43
我认为“&”以某种方式被转换为&。但我不知道我怎么能阻止它做到这一点?是否有任何来自PHP的神奇功能,将阻止它做到这一点? – user2443943 2014-11-24 22:41:49
$ homepage和$ processedHomePage之间正在做什么?我想这是代码缺失。 – hellcode 2014-11-24 22:42:58