我们正在寻找可以使用 PHP DOM 轻松替换值的脚本。这里我们有一个我需要替换的 HTML 代码
HTML 代码
<html>
<head>
<head>
<body>
<div> Explore HISTORY shows, watch videos and full episodes, play games and access articles on historical topics at History.com <p>Miss an episode of your favorite History shows? Go to history.com to catch up on full episodes and video exclusives.</p></div>
<div>Discover what happened today in history. Read about major past events that happened today including special entries on crime, entertainment, and more.</div>
<p>Experience games from your favorite shows, take quizzes, solve puzzles and more!</p>
</body>
</html>
我们必须将“历史”一词(包括粗体/小字符)替换为<u>history</u>
最终的代码是
<html>
<head>
<head>
<body>
<div> Explore <u>HISTORY</u> shows, watch videos and full episodes, play games and access articles on historical topics at <u>History</u>.com <p>Miss an episode of your favorite <u>History</u> shows? Go to <u>history</u>.com to catch up on full episodes and video exclusives.</p></div>
<div>Discover what happened today in <u>history</u>. Read about major past events that happened today including special entries on crime, entertainment, and more.</div>
<p>Experience games from your favorite shows, take quizzes, solve puzzles and more!</p>
</body>
</html>
这是我尝试过的,但它不起作用:
<?php
libxml_use_internal_errors(true);
@$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadHTMLFile('http://www.history.com');
@$body = $doc->getElementsByTagName('body');
$i=0;
while(is_object($finance = $doc->getElementsByTagName("body")->item($i)))
{
foreach($finance->childNodes as $nodename)
{
$node = $doc->createElement("para", "<u>as fasd fasd fadsf</u>");
if(stristr($nodename->nodeValue, 'search')){
$nodename->appendChild($node);
echo $nodename->getAttribute."<br>" ;
echo $nodename->nodeValue."<br>" ;
@$us = true;
}
echo $nodename->nodeValue."<br>" ;
}
$i++;
}
libxml_clear_errors();