gpt4 book ai didi

php正则表达式获取href标签内的字符串

转载 作者:IT王子 更新时间:2023-10-29 00:07:39 25 4
gpt4 key购买 nike

我需要一个正则表达式,它会在 href 标记内和引号内为我提供字符串。

例如我需要在下面提取 theurltoget.com:

<a href="theurltoget.com">URL</a>

此外,我只想要基本 url 部分。 IE。来自 http://www.mydomain.com/page.html 我只想要 http://www.mydomain.com/

最佳答案

不要为此使用正则表达式。您可以使用 xpath 和内置的 php 函数来获得您想要的:

    $xml = simplexml_load_string($myHtml);
$list = $xml->xpath("//@href");

$preparedUrls = array();
foreach($list as $item) {
$item = parse_url($item);
$preparedUrls[] = $item['scheme'] . '://' . $item['host'] . '/';
}
print_r($preparedUrls);

关于php正则表达式获取href标签内的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4001328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com