2012-10-01 50 views
1

我們正在尋找,可以輕鬆替換使用PHP DOM的值的腳本。 這裏我們有一個HTML代碼,我需要更換如何使用PHP Dom替換數據?

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(); 
+0

的可能重複的[忽略的preg_replace html標籤(http://stackoverflow.com/questions/8193327/ignore-html-tags-in-preg-replace) – hakre

+0

你爲什麼要用DOM? – Nin

+0

@Sam:代碼建議是在鏈接問題的答案。你可以在這裏瞭解不區分大小寫的xpath:[在php中不區分大小寫的xpath搜索](http://stackoverflow.com/questions/3238989/case-insensitive-xpath-searching-in-php)。 – hakre

回答

0

使用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"; 

?> 

OUTPUT
<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>