2013-02-07 35 views
0

我想從HTML文件中存在的所有標記中刪除alt標記。實際上,它將刪除許多標籤,但不是很少。我無法理解爲什麼?有誰能夠幫助我。 Thx提前。 下面是我的代碼去除,:

function get_clear_data($html){ 
     $dom = new DOMDocument(); 

     $dom->loadHTML($html); 

     foreach($dom->getElementsByTagName('img') as $image) 
     { 
      $image->removeAttribute('alt'); 
      $image->removeAttribute('xmlns'); 
      $image->removeAttribute('title'); 
     } 

       $final_data=$dom->saveHTML(); 

       return $final_data; 
} 

HTML標記的示例如下:

<img align="middle" alt="«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mo»§#8712;«/mo»«/math»" class="Wirisformula" src="/ckeditor_3.6.1//plugins/ckeditor_wiris/integration/showimage.php?formula=811d8a98dc8c17603bbb0d65bece4423.png" title="Double click to edit"> R define <img align="middle" alt="«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«msup»«mi»e«/mi»«mi»z«/mi»«/msup»«mo»=«/mo»«msup»«mi»e«/mi»«mi»x«/mi»«/msup»«mo»§nbsp;«/mo»«mo»(«/mo»«mi mathvariant=¨normal¨»cos«/mi»«mo»§nbsp;«/mo»«mi»y«/mi»«mo»§nbsp;«/mo»«mo»+«/mo»«mo»§nbsp;«/mo»«mi»i«/mi»«mo»§nbsp;«/mo»«mi mathvariant=¨normal¨»sin«/mi»«mo»§nbsp;«/mo»«mi»y«/mi»«mo»§nbsp;«/mo»«mo»)«/mo»«mo»§nbsp;«/mo»«mi mathvariant=¨normal¨»sin«/mi»«mo»§nbsp;«/mo»«mi»z«/mi»«mo»§nbsp;«/mo»«mo»=«/mo»«mfrac»«mn»1«/mn»«mrow»«mn»2«/mn»«mi»i«/mi»«/mrow»«/mfrac»«mo»(«/mo»«msup»«mi»e«/mi»«mrow»«mi»i«/mi»«mi»z«/mi»«/mrow»«/msup»«mo»-«/mo»«msup»«mi»e«/mi»«mrow»«mo»-«/mo»«mi»i«/mi»«mi»z«/mi»«/mrow»«/msup»«mo»)«/mo»«mo»§nbsp;«/mo»«mo»§nbsp;«/mo»«mi»a«/mi»«mi»n«/mi»«mi»d«/mi»«mo»§nbsp;«/mo»«mi mathvariant=¨normal¨»cos«/mi»«mo»§nbsp;«/mo»«mi»z«/mi»«mo»§nbsp;«/mo»«mo»=«/mo»«mfrac»«mn»1«/mn»«mn»2«/mn»«/mfrac»«mo»(«/mo»«msup»«mi»e«/mi»«mrow»«mi»i«/mi»«mi»z«/mi»«/mrow»«/msup»«mo»+«/mo»«msup»«mi»e«/mi»«mrow»«mo»-«/mo»«mi»i«/mi»«mi»z«/mi»«/mrow»«/msup»«mo»)«/mo»«/math»" class="Wirisformula" src="/ckeditor_3.6.1//plugins/ckeditor_wiris/integration/showimage.php?formula=7044dd31440f7c753195b5b5e09ba431.png" title="Double click to edit"> 
+0

您可以顯示您正在運行該功能的標記嗎? –

+0

是的,我已經添加了示例HTML代碼。 – PHPLover

+0

在我的html樣本上運行你的函數刪除'alt'和'title'屬性。我需要一個更大的樣本來看看我在想什麼。 –

回答

0

同時適用於圖像。你將html存儲在一個變量中,然後將它傳遞給該方法?

相關問題