strip_tags() … replace tags by space rather than deleting them

前端 未结 10 963
日久生厌
日久生厌 2020-12-08 06:36

Do you know how to replace html tags with a space character using php?

If I display

strip_tags(\'

Foo

bar\');

相关标签:
10条回答
  • 2020-12-08 07:05

    Try this.

    preg_replace('#<[^>]+>#', ' ', '<h1>Foo</h1>bar');
    
    0 讨论(0)
  • 2020-12-08 07:05

    With the Regex solution preg_replace('/<[^>]*>/', ' ', $str), it's not gonna work if you have event attributes like this:

    <button onclick="document.getElementById('alert').innerHTML='<strong>MESSAGE</strong>';">
    click</button>
    

    You need to do one more replacement:

    <?php
    
    $str =
    "<div data-contents=\"<p>Hello!</p>\">Hi.</div>".
    "Please<button onclick=\"document.getElementById('alert').innerHTML='".
    "<strong>MESSAGE</strong>';\">click</button>here.";
    
    $event = 
    "onafterprint|onbeforeprint|onbeforeunload|onerror|onhaschange|onload|onmessage|".
    "onoffline|ononline|onpagehide|onpageshow|onpopstate|onresize|onstorage|onunload|".
    "onblur|onchange|oncontextmenu|onfocus|oninput|oninvalid|onreset|onselect|onsubmit|".
    "onkeydown|onkeypress|onkeyup|onclick|ondblclick|ondrag|ondragend|ondragenter|".
    "ondragleave|ondragover|ondragstart|ondrop|onmousedown|onmouseenter|onmousemove|".
    "onmouseleave|onmouseout|onmouseover|onmouseup|onscroll|onabort|oncanplay|".
    "oncanplaythrough|oncuechange|ondurationchange|onemptied|onended|onerror|".
    "onloadeddata|onloadedmetadata|onloadstart|onpause|onplay|onplaying|onprogress|".
    "onratechange|onseeked|onseeking|onstalled|onsuspend|ontimeupdate|onvolumechange|".
    "onwaiting|data-[^=]+";
    
    $str = preg_replace("/<([^>]+)(".$event.")=(\"|')(?:(?!\\3).)+\\3/", "<$1", $str);
    $str = preg_replace("/<[^>]*>/", " ", $str);
    
    echo $str;
    
    // with only strip_tags:
    // Hi.Pleaseclickhere.
    
    // with event and data attributes removal + regex tags removal:
    // Hi. Please click here.
    
    // with only regex tags removal:
    // Hello! ">Hi. Please MESSAGE ';">click here.
    
    ?>
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-08 07:16

    You can try

    $str = '<h1>Foo</h1>bar';
    var_dump(replaceTag($str,array("h1"=>"div")));
    

    Output

    string '<div>Foo</div>bar' (length=17)
    

    Function Used

    function replaceTag($str,$tags) {
        foreach ( $tags as $old => $new )
            $str = preg_replace("~<(/)?$old>~", "<\\1$new>", $str);
        return $str;
    }
    
    0 讨论(0)
  • 2020-12-08 07:17
    preg_replace('#\<(.+?)\>#', ' ', $text);
    

    Bit late with answer but try this one, basically selects everything inside <> including the tags.

    0 讨论(0)
  • 2020-12-08 07:19

    Something like this will work if you know that > won't be in any of your attributes.

    preg_replace('/<[^>]+>/', ' ', 'hello<br>world');

    0 讨论(0)
  • 2020-12-08 07:23

    I helped my self with example from user40521, but I made a function with same api like php's strip_tags, it does not use multiple variables, and it also makes a trim, so a single whitespace is removed from start / end.

    /**
     * @param string $string
     * @param string|null $allowable_tags
     * @return string
     */
    function strip_tags_with_whitespace($string, $allowable_tags = null)
    {
        $string = str_replace('<', ' <', $string);
        $string = strip_tags($string, $allowable_tags);
        $string = str_replace('  ', ' ', $string);
        $string = trim($string);
    
        return $string;
    }
    
    0 讨论(0)
提交回复
热议问题