How to remove only html tags in a string?

后端 未结 5 2183
庸人自扰
庸人自扰 2021-01-04 17:49

I have written code for removing HTML tags, but it is also removing a type of strings. I want it to not to remove strings like 2<3 or

相关标签:
5条回答
  • 2021-01-04 18:03

    Remove all HTML tags from PHP string with content!

    Let say you have string contains anchor tag and you want to remove this tag with content then this method will helpful.

    $srting = '<a title="" href="/index.html"><b>Some Text</b></a> a<b';
    
    echo strip_tags_content($srting);
    
    function strip_tags_content($text) {
    
        return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
        
     }
    

    Output:

    a < b

    Retrieved from: Remove all html tags from php string

    0 讨论(0)
  • 2021-01-04 18:06

    Use strip tags function of php

    echo strip_tags($html)
    
    0 讨论(0)
  • 2021-01-04 18:06

    Strip_tags function is good solution.

    But if you need regex, use expression below.

    (?:<|&lt;)\/?([a-z]+) *[^\/(?:<|&lt;)]*?(?:>|&gt;)

    0 讨论(0)
  • 2021-01-04 18:19

    Use strip_tags

    //If you want to allow some tags
    $term = strip_tags($term,"<b>");
    
    0 讨论(0)
  • 2021-01-04 18:23

    Sorry I had not validate enough.

    I have checked php5-cli expression below.

    (?:<|&lt;)\/?([a-zA-Z]+) *[^<\/]*?(?:>|&gt;)
    

    PHP code goes:

    #!/usr/bin/php 
    <?php
    
    $str = "<html></html>
    a<b 1<2 3>1 
    <body>1>2</body>
    <style file=\"'googe'\" alt=\"google\">hello world</style>
    <have a good efghijknopqweryuip[]asdfgghjkzxcv bnm,.me>hello world<> google com</s>
    <a se=\"font: googe;\">abcde</a>";
    
    echo "text--->".preg_replace('/(?:<|&lt;)\/?([a-zA-Z]+) *[^<\/]*?(?:>|&gt;)/', '', $str)."\n";
    
    ?>
    

    Result:

    text--->
    a<b 1<2 3>1 
    1>2
    hello world
    hello world<> google com
    abcde
    
    0 讨论(0)
提交回复
热议问题