php - 删除重复值节点列表 domdocument PHP
问题描述
我正在尝试通过 Domdocument 删除下面的重复值,但是我该怎么做呢?
下面是设置我想要的值的函数。
所需的输出值将只是
<link href="https://fonts.googleapis.com/css2?family=Overpass" rel="stylesheet">
并不是
<link href="https://fonts.googleapis.com/css2?family=Overpass" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Overpass" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Overpass" rel="stylesheet">
功能
add_filter('the_content', 'idh_change_output_html_ampstor_webstories');
function idh_change_output_html_ampstor_webstories( $content ) {
global $post;
$postID = $post->ID;
if ("web-stories" === $post->post_type && !empty($content)) {
$dom = new DomDocument();
$dom->loadHTML($content);
$links = $dom->getElementsByTagName("link");
if (!empty($links)) {
foreach ($links as $link) {
$string_href = $link->getAttribute("href");
$find_link_google = "fonts.googleapis.com";
$link_canonical = $link->getAttribute("rel") === "canonical";
$link_font_family = $link->getAttribute("rel") === "stylesheet" && strpos($string_href, $find_link_google);
if($link_canonical) {
$link->setAttribute("href", get_permalink( $postID ) );
}
if($link_font_family) {
$link->setAttribute("href", "https://fonts.googleapis.com/css2?family=Overpass");
}
}
}
return $dom->saveHTML();
}
return $content;
}
解决方案
推荐阅读
- python - 可以使用第一个函数访问另一个函数内的函数内声明的列表,但不使用全局属性就不能访问变量
- python - 如何在 Docker 容器中运行 pdb
- php - 在一行中显示多行
- python - 这个批处理 FOR 命令如何转换为 python?
- python - 如何在数据框中显示所有列内容
- angular - 如何在没有角度的其他网站上使用量角器进行 E2E 测试?
- python - 您可以在 SQL SERVER 2016 中将 Python 与 MS 机器学习一起使用吗
- postgresql - Postgres 在使用 OR 过滤器时使用低效的查询计划,在使用 IN 子句时使用正确的索引
- vb.net - 我需要查看什么来解决此打印问题
- c - 如果人多于一,求每个人的最高值和最低值