简体   繁体   English

PHP preg_replace匹配HTML属性

[英]PHP preg_replace match HTML attribute

I'm attempting to remove the title attribute from HTML elements. 我正在尝试从HTML元素中删除title属性。

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);
}

However, it can't tell the difference between <img title="something"> and [shortcode title="something"] . 但是,它无法区分<img title="something">[shortcode title="something"]之间的区别。 How can I target only the code in HTML tags (such as <img> or <a href=""><a> )? 如何仅定位HTML标记中的代码(例如<img><a href=""><a> )?

Do not use regexp, use a DOM parser instead. 不要使用regexp,而是使用DOM解析器。 Go the the official reference page and study it. 进入官方参考页面并进行研究。 In your case you need the DOMElement::removeAttribute() method. 在您的情况下,您需要DOMElement :: removeAttribute()方法。 Here is an example: 这是一个例子:

<?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();

I used the code from @Hast as a building block. 我使用@Hast中的代码作为构建块。 It looks like this does the trick (unless there's a better way?) 看起来这就是诀窍(除非有更好的方法吗?)

/**
 * 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM