如何编辑href链接(刮板)

您所在的位置:网站首页 variables修改器 如何编辑href链接(刮板)

如何编辑href链接(刮板)

2023-03-28 09:58| 来源: 网络整理| 查看: 265

您可以使用正则表达式查找所有链接,然后使用parse_url()和parse_str()重建链接。

例如:

if (preg_match_all('/(.+)/i',$html,$matches)) { // at this point, $matches is a multidimensional array where // index 0 is an array of all matches of the full pattern, // and index 1 is an array of all captured links foreach ($matches[1] as $link) { // parse the link if ($parsed_link = parse_url($link)) { // see the documentation of parse_url() for the various // array keys produced by calling it; in this case we // are using the value of 'query' and passing it to // parse_str() which will break a url query string // into individual variables; pass $arguments as below // and it will populate the result into it as an array parse_str($parsed_link['query'],$arguments); // now, we want the value of the 'newurl' query parameter // from the original url if (isset($arguments['newurl'])) { $new_url = $arguments['newurl']; // do whatever you want with $new_url } } } }

这当然不是要做到这一点的唯一方法,但有使用的一致性和可读性的语言功能的一些价值。我没有在上面的正则表达式中寻找链接,因此它不处理任何特殊情况。如果文档中的链接格式不正确,则可能需要修改该表达式以处理多余的空格,放错位置的引号等。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3