Like the function strip_tags()

Discussion in 'PHP' started by AT-XE, Jun 28, 2008.

  1. #1
    Hello guys,
    You know the function strip_tags() right?
    It removes all the tags and the content inside them, is there any way that I can remove tags but leaving the content inside them?

    Thank you,
    -AT-XE
     
    AT-XE, Jun 28, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    function strip_tags leaves the data data intact, but just removes the usual tags. You might have error somewhere, see example below please:

    <?
    
    	$str = "<b>Hi am Vooler</b>, and am a php teacher.";
    	
    	$str = strip_tags($str);
    
    	echo $str; //printed out:   Hi am Vooler, and am a php teacher.
    
    ?>
    PHP:
    Or show me your code.

    regards
     
    Vooler, Jun 28, 2008 IP
  3. Ikki

    Ikki Peon

    Messages:
    474
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There must be something wrong with your code, AT. strip_tags() won't wipe out your content just like that.
     
    Ikki, Jun 28, 2008 IP
  4. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hmm, found a solution and (!!!) it removes javascript too:

    <?php
    $file = file_get_contents('http://www.google.com/');
    function html2txt($document){
    $search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
                   '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
                   '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
                   '@<![\s\S]*?--[ \t\n\r]*>@'        // Strip multi-line comments including CDATA
    );
    $text = preg_replace($search, '', $document);
    $txt = str_replace("\r\n", '', $txt);
    return $text;
    }
    echo html2txt($file);
    ?>
    PHP:
     
    AT-XE, Jun 28, 2008 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Just use strip_tags. Much more efficient than the function you have.

    Peace,
     
    Barti1987, Jun 28, 2008 IP
  6. chaosprime

    chaosprime Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If strip_tags() gets rid of your content, your tags are probably miswritten -- unclosed quotes on a tag attribute could conceivably do that.
     
    chaosprime, Jun 28, 2008 IP
  7. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #7
    Well, I will suggest use the standard one as azizny is suggesting as well as corruption of returned data is unexpected on different forms of input as chaosprime quoted, your script is making call to 3 functions practically.

    1.html2txt -> 2.preg_replace -> 3.str_replace

    Comparing precompiled function strip_tags is inline and filters internally rather than making call to companion functions. For the time being, it is ok, but when you get into large applications, where you have to keep structure of applciation stable, you will be feeling need of what is being said. Just a thought.

    regards
     
    Vooler, Jun 28, 2008 IP
  8. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The problem with strip tags is that it leaves the content, yes but it allows some style information to pass, sometimes and javascript.
     
    AT-XE, Jun 30, 2008 IP