如何使用php domdocument检索和修改具有“红色文本”的链接(“ href”在此Wikipedia Infobox页面上?

发布于 2025-02-06 07:55:12 字数 1922 浏览 1 评论 0原文

我正在尝试检索和修改红色文本URL的链接(包括: autonavi,ucweb agtech Holdings Limited )这里:

“

我的代码允许我通过 domdocument ,所有href属性/wiki/code> in所有a文档的标签或字符串的网页:$ urlsearch = base_path。 str_ireplace

libxml_use_internal_errors(true);
$parser = new DOMDocument();
$parser->loadHTMLFile("https://fr.wikipedia.org/wiki/Alibaba_Group");
$get_a_tags = $parser->getElementsByTagName("a");
foreach ($get_a_tags as $get_a_tag) {
                if (stripos($get_a_tag->getAttribute('href'), "/wiki/") !== false || stripos($get_a_tag->getAttribute('href'), "#") !== false) {
                    $get_href_in_a_infobox = $get_a_tag->getAttribute('href');
                    $term = $get_a_tag->nodeValue;
                    $urlSearch = BASE_PATH."search.php?term=$term&type=sites";
                    // var_dump($urlSearch."<br><br>");
                    $wikipediaInfoboxTable = str_ireplace($get_href_in_a_infobox, $urlSearch, $wikipediaInfoboxTable);
                }
            }

我的代码正常工作。

但是,问题是,当我重现同一件事以检索其HREF中包含的URL时,字符串/w/index.php?,甚至redlink = 1,简单地做:if(stripos($ get_a_tag-&gt; getAttribute('href') ,“ /w/index.php?”)!== false ||($ get_a_tag-&gt; getAttribute('href'),“ redlink = 1”)!== false)我没有注意到此类更改在上一个CSS类的代码中成功的任何更改,该类别包含 /wiki/wiki/ 更改。

如何成功修改链接(HREF属性)的所有标签都有CSS类new ???

换句话说,我如何像上面的代码一样成功修改包含字符字符串/w/index.php?的链接(href属性) redlink = 1 ???

我真的需要你的帮助。

I'm trying to retrieve and modify the link of red text URLs (including: AutoNavi, UCWeb and AGTech Holdings Limited) at the infobox level here:

enter image description here

My code below allows me to replace via DomDocument, all the href attributes containing /wiki/ in all the a tags of the Document or the Web Page by the character string: $urlSearch = BASE_PATH."search.php?term=$term&type=sites" with str_ireplace:

libxml_use_internal_errors(true);
$parser = new DOMDocument();
$parser->loadHTMLFile("https://fr.wikipedia.org/wiki/Alibaba_Group");
$get_a_tags = $parser->getElementsByTagName("a");
foreach ($get_a_tags as $get_a_tag) {
                if (stripos($get_a_tag->getAttribute('href'), "/wiki/") !== false || stripos($get_a_tag->getAttribute('href'), "#") !== false) {
                    $get_href_in_a_infobox = $get_a_tag->getAttribute('href');
                    $term = $get_a_tag->nodeValue;
                    $urlSearch = BASE_PATH."search.php?term=$term&type=sites";
                    // var_dump($urlSearch."<br><br>");
                    $wikipediaInfoboxTable = str_ireplace($get_href_in_a_infobox, $urlSearch, $wikipediaInfoboxTable);
                }
            }

My code above works fine.

BUT, the problem is that when I reproduce the same thing to retrieve the URLs containing in their href, the string /w/index.php? or even redlink=1 by simply doing: if (stripos ($get_a_tag->getAttribute('href'), "/w/index.php?") !== false || stripos($get_a_tag->getAttribute('href'), "redlink=1") !== false), I don't notice any changes like this was successful in the previous code for the CSS class containing the word /wiki/ change.

How to successfully modify the link (href attribute) of all a tags having CSS class new???

In other words, how can I successfully modify, as I did in my code above, the links (href attribute) containing the character strings /w/index.php? and redlink=1 ???

I really need your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2025-02-13 07:55:12

$ wikipediainfoboxtable实际上包含Infobox的表。如您所说,您会给我什么建议直接在DOM中工作???

因此,如何在我的代码案例中替换href,而无需使用str_ireplace ???

$wikipediaInfoboxTable actually contains the table of infobox. What suggestions do you give me to work directly in the DOM as you say ???

So, how do I replace the href in my code case without using the str_ireplace???

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文