如何使用PHPDOM替换数据?
我们正在寻找一个脚本,它可以使用PHP DOM轻松地替换值。 这里我们有一个HTML代码,我需要替换它 HTML代码如何使用PHPDOM替换数据?,php,dom,domdocument,Php,Dom,Domdocument,我们正在寻找一个脚本,它可以使用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
<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>
我们必须替换“历史”一词,包括粗体/小字符。历史
最后的代码是
<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();
使用DOMXPath查找包含单词history的节点,不区分大小写,然后将其拆分为新的文本节点 出于好奇,我继续写了一个实现。我花的时间比我计划的要长,但它确实有效。我希望这对你有帮助
<?php
$doc = new DOMDocument();
$doc->preserveWhiteSpace = FALSE;
$doc->resolveExternals = FALSE;
$doc->loadHTML(<<<END
<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>
END
);
echo '<p>Input:</p>'."\n";
echo $doc->saveHTML()."\n";
$word = 'history';
$lcWord = strtolower($word);
$wordLen = strlen($word);
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('/html/body//text()['.
'contains('.
'translate(.,"'.strtoupper($word).'","'.$lcWord.'"),'.
'"'.$lcWord.'")'.
']');
foreach ($nodes as $node)
{
// Split all occurances of "word" into new text nodes.
$text = $node->data;
$textPos = 0;
while (($wordPos = stripos($text,$word)) !== FALSE)
{
$beforeText = substr($text,$textPos,$wordPos - $textPos);
$wordText = substr($text,$wordPos,$wordLen);
// Add the before text to the DOM.
$node->parentNode->insertBefore($doc->createTextNode($beforeText),$node);
// Add the word text to the DOM.
// Underline this word.
$uNode = $doc->createElement('u');
$uNode->appendChild($doc->createTextNode($wordText));
$node->parentNode->insertBefore($uNode,$node);
// Repeat for the text after the word.
$text = substr($text,$wordPos + $wordLen);
}
// Create a text node for text following the word.
if ($text)
$node->parentNode->insertBefore($doc->createTextNode($text),$node);
// Remove the original text node.
$node->parentNode->removeChild($node);
}
echo '<p>Output:</p>'."\n";
echo $doc->saveHTML()."\n";
?>
输出
链接问题的答案中可能有重复的@Sam:Code建议。您可以在这里了解不区分大小写的xpath:。一个澄清是因为有人决定先否决投票,然后再问问题——或者根本不提问题,dunno:显示的数据是如何存储的?如果它只是一个字符串,那么在这里使用DOM解析就太过分了,因为这里不解析任何东西。如果它是在DOM结构中,我仍然认为你最好使用preg_replace而不是它的HTML表示形式,如果这是它应该完成的最后一项任务的话。@raina77ow:那么,这里已经概述了preg_replace:@Sam:如果你展示你到目前为止所做的尝试,也会很有帮助。请求访问通常是离题的。网站的主要目的是编码方法,而不是现成的解决方案,也不是其本身。即使我有时也会违反这条规则:
<p>Input:</p>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<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>
<p>Output:</p>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<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>