【发布时间】:2020-10-12 01:42:21
【问题描述】:
基本上,我的变量包含 html 代码,其中有一个我尝试捕获的 url。 我能够捕获它,但我忘记了哪些标志或方法将组包含在匹配项中
$xxx = 'icon-2525 attributeIcon-2521" focusable="false" height="100%" role="img"
width="100%"><use xlink:href="#icon-clipboard"></use></svg><dl class="itemAttribute-1524"><dd
class="attributeValue-3783238563"><a class="attributeLink-387024144" rel=" noopener noreferrer"
href="https://tld.example.com/en/main?id=325235231763265&ref=testetghe"
title="Opens in a new window." target="_blank"';
preg_match_all("/https:\/\/tld.example.com(.*?)ref=testetghe\"/is", $xxx, $matches);
foreach($matches[1] as $url);
foreach 会将 $url 正确设置为 (/en/main?id=325235231763265&) 但我希望 $url 成为完整的 url (https://tld.example.com/en/main?id=325235231763265&ref=testetghe)
我会怎么做?这是我必须改变的标志吗?我在 regex101 上尝试了很多 在这里它的工作 https://regex101.com/r/jguNDZ/1
【问题讨论】:
-
当你的名字是
DOMDocument时很有趣,但是使用正则表达式来解析HTML,使用索引零来获得匹配 -
感谢 Kevin,但变量也可能包含 xml、csv、制表符分隔、超级循环、数据头等