- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想将链接列表(数组中的 hrefs)中的单词列表(数组中)替换为 html 页面。
我认为主要有 2 个选择:
通过正则表达式执行此操作(强烈建议不要解析和更改 html)。
使用 html 解析器并遍历 DOM 以替换每个单词和链接列表。
第二个选项的问题如下:
我不想替换之前在 html 页面中创建的链接,我必须知道在标签所在的列表中找到的每个单词。
我不想替换 DOM 每个节点上的单词,只替换没有子节点的节点,即只替换叶子中的节点。
简单示例:
$aURLlist = array('www.google.com','www.facebook.com');
$aWordList = array('Google', 'Facebook');
$htmlContent='<html><body><div>Google Inc. is an American multinational corporation specializing in Internet-related services and products.</div><div>Facebook is an online social networking service, whose name stems from the colloquial name for the book given to students at the start of the academic year by some university administrations in the United States to help students get to know each other.</div></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($htmlContent);
$htmlContent=walkingDom($dom,$aURLlist,$aWordList); //replace all words of $aWordList found in text nodes of $dom TO links with href equal to URL in $aURLlist
结果:
$htmlContent=<html><body><div><a href='www.google.com'>Google</a> Inc. is an American multinational corporation specializing in Internet-related services and products.</div><div><a href='www.facebook.com'>Facebook</a> is an online social networking service, whose name stems from the colloquial name for the book given to students at the start of the academic year by some university administrations in the United States to help students get to know each other.</div></body></html>';
我有一个使用 DOMDocument 库遍历 DOM 的递归函数,但我无法附加“ anchor ”节点来替换在叶“文本”节点中找到的单词。
function walkDom($dom, $node, $element, $sRel, $sTarget, $iSearchLinks, $iQuantityTopics, $level = 0, $bLink = false) {
$indent = '';
if ($node->nodeName == 'a') {
$bLink = true;
}
for ($i = 0; $i < $level; $i++)
$indent .= ' ';
if ($node->nodeType != XML_TEXT_NODE) {
//echo $indent . '<b>' . $node->nodeName . '</b>';
//echo $indent . '<b>' . $node->nodeValue . '</b>';
if ($node->nodeType == XML_ELEMENT_NODE) {
$attributes = $node->attributes;
foreach ($attributes as $attribute) {
//echo ', ' . $attribute->name . '=' . $attribute->value;
}
//echo '<br>';
}
} else {
if ($bLink || $node->nodeName == 'img' || $node->nodeName == '#cdata-section' || $node->nodeName == '#comment' || trim($node->nodeValue) == '') {
continue;
//echo $indent;
//echo 'NO replace: ';
//var_dump($node->nodeValue);
//echo '<br><br>';
} elseif (!$bLink && $node->nodeName != 'img' && trim($node->nodeValue) != '') {
//echo $indent;
//echo "TEXT TO REPLACE: $element, $replace, $node->nodeValue, $iSearchLinks <br>";
$i = 0;
$n = 1;
while (i != $iSearchLinks && $n > 0 ) {
//echo "Create link? <br>";
$node->nodeValue = preg_replace('/'.$element->name.'/', '', $node->nodeValue, 1, $n);
if ($n > 0) {
//echo "Creating link with $element->name <br>";
$link = $dom->createElement("a", $element->name);
$link->setAttribute("class", "nl_tag");
$link->setAttribute("id", "@@ID@@");
$link->setAttribute("hreflang", $element->type);
$link->setAttribute("title", $element->altname);
$link->setAttribute("href", $element->resource);
if ($sRel == "nofollow") $link->setAttribute("rel", $sRel);
if ($sTarget == "_blank") $link->setAttribute("target", $sTarget);
$node->parentNode->appendChild($link);
//var_dump($node->parentNode);
$dom->encoding = 'UTF-8';
$dom->saveHTML();
$iQuantityTopics++;
}
$i++;
//saveHTML?
//echo '<br><br>';
}
}
}
此解决方案不起作用,因为 appendChild 函数仅在子项的末尾添加新的子项,但我想将其添加到找到的要替换的单词所在的位置。
我还尝试将带有 preg_replace 函数的链接直接添加到叶文本节点中,但是 anchor 作为“文本格式”添加到文本节点中,我需要将其添加为链接节点以替换叶中的单词所在的文本节点。
我的问题是:是否可以使用 PHP 中的 html 解析器来执行此操作,或者我必须求助于正则表达式?提前致谢!
最佳答案
@Suamere:
“我不确定 PHP 引擎不支持什么:(?i)(?<!<[^>]*|>)(strWord)(?!<|[^<]*>) "(?i)
- 是的,尽管将 i 放在最后会更容易:
/(someregex)/i<br>
(?<!<[^>]\*|>)
您在这里寻找前导标签;我通过删除第一个 <(某种)
所以这就是最终的正则表达式的样子,它尽可能接近你想要做的事情:
/(?!<[^>]\*>).\*(strWord).\*(?!<\/[^<]\*>)/i<br>
但是,更简单的方法是:
$text = "...";<br>
$words = array('him', 'her', ...);<br>
$links = array('<a href="...">$0</a>', ...);<br>
foreach ($words as $word) {<br>
 array_push($regexes, "/\b{$word}\b/i");<br>
}<br>
$modified_array = preg_replace($regexes, $links, $text);<br>
重要的是 $words 和 $links 具有完全相同数量的元素;否则会抛出错误。
$0 引用对应正则表达式的整个匹配;在这种情况下,只有您要查找的特定词本身。
此外,preg_replace()
默认应用/g 修饰符,因此每个正则表达式都不需要该修饰符。 :-)
关于php - PHP 中的 Walking Dom 将找到的字符串列表替换为 "HTML text",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16439878/
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
Polymer的light DOM和local DOM有什么区别?来自文档(1): The DOM that an element creates and manages is called its
当内容添加到网页时,我需要触发一个 Action 。更新可能具有不同的性质(例如 AJAX、延迟脚本、用户操作)并且不受我的控制。 我想使用 DOM 突变事件,但它们并非在所有浏览器中都可用。是否有为
我刚遇到一个有趣的情况,我有一个提交 放置在 内的 native 自定义元素的 Shadow DOM 内. Select #shadow-root ...
假设有一个滚动列表,当我插入一些新的 DOM 附加到当前 dom 时,它工作正常。上拉 但是如果我之前插入一些新的 DOM,新的 DOM 将在视口(viewport)中,而旧的 DOM 将被下推。下拉
在我的项目中实现 Shadow DOM 是否会使它们像 React 使用的虚拟 DOM 一样更快? 最佳答案 它们是不同用途的不同事物,因此比较性能没有意义。 虚拟 DOM 虚拟 DOM 旨在避免对
在我的页面内容上,我将多张卡片组织成网格 __________________ | ____ ____ | | | | | | | | | | | |
是否可以在浏览器中看到(调试)从 DOM 元素触发的自定义事件? 假设我想查看 Bootstrap Collapse 的哪个特定元素触发了 show.bs.collapse event ,我能以某种方
我正在生成用于客户端的 XPaths 服务器端,我很困惑为什么在 DOM 中找不到表路径(即 td 中的内容)。 事实证明,现代浏览器(至少是 Chrome 和 Firefox)插入了 tbody在文
是否可以检索文本节点的几何位置(即从父元素、页面等的顶部/左侧偏移量)? 最佳答案 不是直接的。 TextNode 没有用于测量视口(viewport)定位的原始 IE 偏移*(和类似的)扩展。 仅在
以下语句中的 DOM 元素的含义是什么? Statement #1 You can add multiple classes to a single DOM element. Statement #2
有没有办法让 firebug(或任何其他浏览器,或使用任何其他工具)阻止任何 dom 操作的发生?有时布局调试充满悬停事件的屏幕是不可能的,因为元素可能会消失,并且您看不到它们的复合布局。 最佳答案
我需要在html文档中搜索 text here 然后输出完整的节点路径(CSS或XPATH) 例如 html > body > div class ="something" > table > tr
这是我的一个页面的典型加载时间如何拆分为:- Domain Lookup 0 0 % Connect 134 .3% Request
我的 .on() 工作时遇到一些问题。我的网站是here . 如果你看看 www.eliteweb-creation.co.uk/dev/js/nav.js,我正在 mouseenter 和 mous
我是 Javascript 的新手,负责将我们产品的 UI 从 YUI2 迁移到 YUI3。看起来哪里都没有迁移指南,所以我现在正在浏览互联网帖子和 yui 文档。 在我的全局范围内,我临时添加了类似
我想和实习生一起测试一些 DOM 相关的东西,不需要特定的固定装置,只是一般的 DOM 东西,比如我改变了 Element.prototype。这是否需要通过本地 Selenium 服务器(或 sau
我是 HTML 和 HTML5 的初学者。 当我阅读以下内容时 link ,我找到了术语 DOM 和 DOM API。我通读了维基百科,但无法理解其背后的全部思想。 谁能给我解释一下: 文档对象模型
我有两个主要问题。 Object 之类的扩展是否算数? 什么是 DOM 包装? http://perfectionkills.com/whats-wrong-with-extending-the-do
对不起查询,原型(prototype),雅虎 YUI,道场在考虑小的时候不吸引我。我想要一个模块化的库,代码尽可能小,最多 20Kb [un compressed] 是我所期望的。应该提供 Dom 操
我是一名优秀的程序员,十分优秀!