2012-11-09 36 views
0

我有一個問題。 PHP文件響應500錯誤。不明白是什麼問題。 PHP的錯誤信息是:DOMDocument :: replaceChild()響應結果500(內部服務器錯誤)

Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error'

代碼如下:

$dom = new domDocument; 
$dom->preserveWhiteSpace = false; 
$dom->loadHTML($cont); 

$links = $dom->getElementsByTagName('a'); 
$images = $dom->getElementsByTagName('img'); 
$divs = $dom->getElementsByTagName('div'); 

foreach ($links as $link) 
{ 
    $link->setAttribute('target','_blank'); 
} 

foreach ($images as $image) 
{ 
    $image->setAttribute('style','max-width:405px'); 
} 

foreach ($divs as $div) 
{ 
    $pnode = $dom->createElement('p', $div->nodeValue); 
    $dom->replaceChild($pnode, $div); //the problem string 
} 

更新:如果問題字符串評論沒有錯誤。

+6

始終以'display_errors'開啓發展。 500是PHP中的一個致命錯誤,並且該消息將被清除。 'ini_set('display_errors',1); error_reporting(E_ALL);' –

+0

可能的問題:1)錯誤的htaccess文件,2)試圖訪問代碼3中不存在的方法)錯誤可能與您列出的代碼無關。發表完整的例子,讓人們可以測試它 – Serg

+1

好吧,現在它寫道:致命錯誤:未捕獲的異常'DOMException'與消息'未找到錯誤' –

回答

0

感謝air4x

replaceChildren只查找一個級別。在我的解決方案中,它正在尋找和根中的元素。

解決方案低於:

$div->parentNode->replaceChild($pnode, $div); 
0

代碼似乎沒問題。檢查在phpinfo();中是否啓用了DOM/XML php擴展。將

ini_set('display_errors', 1); 
error_reporting(E_ALL); 

添加到您的代碼或enable php error showing in .htaccess