在PHP中遍历Dom以替换在“中”找到的字符串列表;“HTML文本”;

在PHP中遍历Dom以替换在“中”找到的字符串列表;“HTML文本”;,php,html,regex,dom,replace,Php,Html,Regex,Dom,Replace,我想将链接列表(数组中的HREF)中的单词列表(数组中的)替换为html页面 我认为主要有两种选择: 从正则表达式执行此操作(强烈反对解析和更改html) 使用html解析器并遍历DOM中要替换的每个单词和链接列表 第二个选项的问题如下: 我不想替换以前在html页面中创建的链接,我必须知道标签所在列表中的每个单词 我不想替换DOM中每个节点上的单词,只替换没有子节点的节点,即只替换叶子中的单词 简单示例: $aURLlist = array('www.google.com','www.face

我想将链接列表(数组中的HREF)中的单词列表(数组中的)替换为html页面

我认为主要有两种选择:

  • 从正则表达式执行此操作(强烈反对解析和更改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>';
    
    $aurlist=array('www.google.com','www.facebook.com');
    $aWordList=array('Google','Facebook');
    $htmlContent='Google Inc.是一家专门从事互联网相关服务和产品的美国跨国公司。Facebook是一种在线社交网络服务,这本书的名字来源于美国一些大学管理部门在学年开始时为帮助学生相互了解而给学生起的一个通俗名称;
    $dom=新的DOMDocument();
    $dom->loadHTML($htmlContent);
    $htmlContent=walkingDom($dom、$aurlist、$aWordList)//将在$dom的文本节点中找到的$aWordList的所有单词替换为href等于$Aurlist中的URL的链接
    
    结果:

    $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>';
    
    $htmlContent=Inc.是一家专门从事互联网相关服务和产品的美国跨国公司。是一家在线社交网络服务,其名字来源于美国一些大学管理部门在学年初给学生的书的口语名称,以帮助学生相互了解;
    
    我有一个递归函数,它使用DOMDocument lib遍历DOM,但我不能附加一个“锚”节点来替换在叶“文本”节点中找到的单词

    函数walkDom($dom、$node、$element、$sRel、$sTarget、$iSearchLinks、$iQuantityTopics、$level=0、$bLink=false){
    $indent='';
    如果($node->nodeName=='a'){
    $bLink=true;
    }
    对于($i=0;$i<$level;$i++)
    $indent.='';
    如果($node->nodeType!=XML\u TEXT\u node){
    //回显$indent.'.$node->nodeName.';
    //回显$indent.'.$node->nodeValue.';
    if($node->nodeType==XML\u元素\u节点){
    $attributes=$node->attributes;
    foreach($attributes作为$attribute){
    //echo','.$attribute->name'='.$attribute->value;
    }
    //回声“
    ”; } }否则{ 如果($bLink | |$node->nodeName=='img'| |$node->nodeName=='| cdata节'| |$node->nodeName=='|注释'| |修剪($node->nodeValue==''){ 继续; //echo$indent; //回应“无替换:”; //变量转储($node->nodeValue); //回音“

    ”; }elseif(!$bLink&&$node->nodeName!='img'&&trim($node->nodeValue)!=''){ //echo$indent; //echo“要替换的文本:$element,$REPLACE,$node->nodeValue,$iSearchLinks
    ”; $i=0; $n=1; 而(i!=$iSearchLinks&&$n>0){ //回显“创建链接”
    ; $node->nodeValue=preg_replace('/'.$element->name'/',''.$node->nodeValue,1,$n); 如果($n>0){ //echo“使用$element->name创建链接
    ”; $link=$dom->createElement(“a”,$element->name); $link->setAttribute(“类”、“nl_标记”); $link->setAttribute(“id”,“@@id@@@”; $link->setAttribute(“hreflang”,$element->type); $link->setAttribute(“标题”、$element->altname); $link->setAttribute(“href”,$element->resource); 如果($sRel==“nofollow”)$link->setAttribute(“rel”,$sRel); 如果($sTarget==“_blank”)$link->setAttribute(“target”,即$sTarget); $node->parentNode->appendChild($link); //变量转储($node->parentNode); $dom->encoding='UTF-8'; $dom->saveHTML(); $iQuantityTopics++; } $i++; //保存HTML? //回音“

    ”; } } }
    这个解决方案不起作用,因为appendChild函数只在子项的末尾添加新的子项,但我想在找到的要替换的单词所在的位置添加它

    我还尝试将带有preg_replace函数的link directy添加到叶文本节点中,但是锚点作为“文本格式”添加到文本节点中,我需要将其添加为链接节点,以替换叶文本节点中的单词

    我的问题是:用PHP中的html解析器是否可以做到这一点,或者我必须求助于正则表达式? 提前感谢!

    @Suamere:


    “我不确定PHP引擎不支持什么:(?I)(?]*|>)(strWord)(?!有趣。我很好奇你到底是什么意思。在服务器端PHP中,你有一个字符串数组,其中包含引用URL。你想替换这些URL中的特定单词(由另一个字符串数组中的单词数组指定)还有其他的。然后你用修改过的HREF数组放在你的HTML页面上?无论如何,一些代码片段会让这变得更加有趣和清晰,lol。你已经理解了我的问题。这个想法是从单词列表和URL列表中自动创建链接,替换HTML页面中为corres找到的给定列表中的所有单词从URL列表中收集链接。啊,我明白了。我想有一些工具可以做到这一点,但这听起来很公平。我会为你发布一些东西。我似乎找不到
    /(?!<[^>]\*>).\*(strWord).\*(?!<\/[^<]\*>)/i<br>
    
    $text = "...";<br>
    $words = array('him', 'her', ...);<br>
    $links = array('&lt;a href="...">$0&lt;/a>', ...);<br>
    
    foreach ($words as $word) {<br>
    &emsp;array_push($regexes, "/\b{$word}\b/i");<br>
    }<br>
    $modified_array = preg_replace($regexes, $links, $text);<br>