我将此html作为示例
this is some html code, and this is <a href="domain.com" title="html">html</a>
this is image <img src="any url with html word" alt="html" />
<iframe src="html"></iframe>
<script type="text/javascript">
var html = "any thing here";
var x = "this is html"
</script>
我希望用html
<a href="domain.com/search/html">html</a>
字
正如我们所看到的,它可能在html标记属性中,我们必须排除所有这些替换的机会,如果它是span或p或div中的纯文本,则只需替换此单词
我尝试了所有方法,但没办法
$dom = new DOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
$query_entries = $xpath->evaluate("(//div | //span | //p)[not(ancestor::a)]/text()");
foreach($query_entries as $element){
if($element instanceof DOMText){
$element->nodeValue = str_replace('html','<a href="domain.com/search/html">html</a>',$element->nodeValue);
}
}
当我用html替换nodeValue时,它将其转义,如果我尝试解码它,则会在js代码中出错
任何正则表达式解决方案?