2013-03-06 61 views
1

我試圖從HTML元素中刪除標題屬性。PHP preg_replace匹配HTML屬性

function remove_title_attributes($input) { 
    return remove_html_attribute('title', $input); 
} 

/** 
* To remove an attribute from an html tag 
* @param string $attr the attribute 
* @param string $str the html 
*/ 
function remove_html_attribute($attr, $str){ 
    return preg_replace('/\s*'.$attr.'\s*=\s*(["\']).*?\1/', '', $str); 
} 

但是,它不能告訴<img title="something">[shortcode title="something"]之間的差異。我如何只定位HTML代碼中的代碼(例如<img><a href=""><a>)?

+3

使用HTML解析器,而不是正則表達式函數。 – 2013-03-06 16:29:28

+4

**不要使用正則表達式來解析HTML **。您無法可靠地使用正則表達式解析HTML。只要HTML從你的期望改變,你的代碼就會被破壞。有關如何使用PHP模塊正確解析HTML的示例,請參閱http://htmlparsing.com/php.html。 – 2013-03-06 16:30:02

+1

[How to parse and process HTML/XML with PHP?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-xml-with-php) – Quentin 2013-03-06 16:43:37

回答

0

我使用@Hast的代碼作爲構建塊。它看起來像這樣做(除非有更好的方法?)

/** 
* To remove an attribute from an html tag 
* @param string $attr the attribute 
* @param string $str the html 
*/ 
function remove_html_attribute($attr, $input){ 
    //return preg_replace('/\s*'.$attr.'\s*=\s*(["\']).*?\1/', '', $input); 

    $result=''; 

    if(!empty($input)){ 

     //check if the input text contains tags 
     if($input!=strip_tags($input)){ 
      $dom = new DOMDocument(); 

      //use mb_convert_encoding to prevent non-ASCII characters from randomly appearing in text 
      $dom->loadHTML(mb_convert_encoding($input, 'HTML-ENTITIES', 'UTF-8')); 

      $domElement = $dom->documentElement; 

      $taglist = array('a', 'img', 'span', 'li', 'table', 'td'); //tags to check for specified tag attribute 

      foreach($taglist as $target_tag){ 
       $tags = $domElement->getElementsByTagName($target_tag); 

       foreach($tags as $tag){ 
        $tag->removeAttribute($attr); 
       } 
      } 

      //$result = $dom->saveHTML(); 
      $result = innerHTML($domElement->firstChild); //strip doctype/html/body tags 
     } 
     else{ 
      $result=$input; 
     } 
    } 

    return $result; 
} 

/** 
* removes the doctype/html/body tags 
*/ 
function innerHTML($node){ 
    $doc = new DOMDocument(); 
    foreach ($node->childNodes as $child) 
    $doc->appendChild($doc->importNode($child, true)); 

    return $doc->saveHTML(); 
} 
3

不要使用正則表達式,而是使用DOM解析器。去official reference page並研究它。在你的情況下,你需要DOMElement::removeAttribute()方法。以下是一個示例:

<?php 

$html = '<p>stuff <a href="link" title="something">linkme</a></p><p>more stuff</p><p>even more stuff</p>'; 

$dom = new DOMDocument(); 
$dom->loadHTML($html); 

$domElement = $dom->documentElement; 

$a = $domElement->getElementsByTagName('a')->item(0); 
$a->removeAttribute('title'); 

$result = $dom->saveHTML(); 
+0

當沒有根標籤時,這不起作用。我正在使用的HTML代碼是一個頁面或博客文章的內容。例如,HTML代碼,我已經是這樣的:'

東西linkme

更多的東西

甚至更​​多的東西

' – 2013-03-06 16:51:52

+0

@ForceFlow我已經更新的例子,它爲我工作。 – Hast 2013-03-06 16:57:41

+0

你說「你需要DOMNode :: removeChild()方法」,但是在你使用的代碼中removeAttribute – naomi 2013-03-06 16:59:49