PHP removing html tags from string

后端 未结 6 1818
小鲜肉
小鲜肉 2020-12-02 02:06

I have string:

Vers­lo cent­rai Lie­tu­vos ne­kil­no­ja­mo­jo turto plėt­ros aso­cia­ci­jos kon­kur­se ...

相关标签:
6条回答
  • 2020-12-02 02:16

    This will remove every thing - tags, ascii, line breaks but pure text:

    strip_tags(preg_replace('/<[^>]*>/','',str_replace(array("&nbsp;","\n","\r"),"",html_entity_decode($YOUR_STRING,ENT_QUOTES,'UTF-8'))));
    
    0 讨论(0)
  • 2020-12-02 02:18

    From PHP 7.4.0 the strip_tags() alternatively accepts an array with allowable tags,

    then this:

    <?php
    
    $html = '<div id="my-div"><p>text<strong><a href="#link"></a></strong></p></div>';
    
    echo strip_tags($html, ['p', 'a']); //accept p and a tags
    

    Return this:

    <p>text<a href="#link"></a></p>
    

    Note that only the disallowed tags have been removed.

    0 讨论(0)
  • 2020-12-02 02:20

    Since your HTML is not properly formatted you could choose a preg_replace() approach:

    $text = '<p justify;"="">Vers­lo cent­rai Lie­tu­vos ne­kil­no­ja­mo­jo turto plėt­ros aso­cia­ci­jos kon­kur­se ... </p>';
    $content = preg_replace('/<[^>]*>/', '', $text); 
    var_dump($content);
    // string(108) "Vers­lo cent­rai Lie­tu­vos ne­kil­no­ja­mo­jo turto plėt­ros aso­cia­ci­jos kon­kur­se ... "
    

    Codepad Example

    On strip_tags() docs it says: Because strip_tags() does not actually validate the HTML, partial or broken tags can result in the removal of more text/data than expected.

    Also second parameter is for $allowable_tags.

    0 讨论(0)
  • 2020-12-02 02:27

    This will replace all html tags, https://regex101.com/r/jM9oS4/4

    preg_replace('/<(|\/)(?!\?).*?(|\/)>/',$replacement,$string);
    
    0 讨论(0)
  • 2020-12-02 02:33

    Try to put it like that

    $content = strip_tags($text);
    

    Or you can do it with regular expression like that:

    $content = preg_replace('/<[^>]*>/', '', $text);
    

    By this $content = strip_tags($text, '<p>'); you are allowing the <p> tag in the string.

    For more info see the link http://php.net/manual/en/function.strip-tags.php

    0 讨论(0)
  • 2020-12-02 02:36

    Since the HTML is poorly formated you probably need to either write your own regexp to remove tags or clean up the HTML before trying to remove tags.

    You could try this to remove everything that "looks like" a tag:

    $str = preg_replace("/<.*?>/", " ", $str);
    
    0 讨论(0)
提交回复
热议问题