SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there an opposite to strip_tags?

    As title really, for example:

    Code:
    strip_tags($text, '<img>') //strips all tags other than the image tag
    However, I need it to do the reverse i.e accept all tags, but strip any image tags from $text. If it existed the function might work something like this:
    Code:
    allow_tags($text, '<img>') //allows all tags, but strips image tags
    Does such a function or something like it exist as I can't find it. I'm at the planning stages of a mobile version of my site and thought that if I could strip images from my page content it would help things load faster for the user.

  2. #2
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There isn't as far as I know, you would have to write your own function to do that. Here is some weird function I found in my archive, maybe it can be of some use?

    PHP Code:
        function stripTagsAttributes($string$allowedTags null$allowedAttributes null) {
            if (
    $allowedAttributes) { 
                if (!
    is_array($allowedAttributes)) {
                    
    $allowedAttributes explode(','$allowedAttributes);
                }
                if (
    is_array($allowedAttributes)) {
                    
    $allowedAttributes implode('|'$allowedAttributes);
                }
                
    $rep '/([^>]*) (' $allowedAttributes ')(=)(\'.*\'|".*")/i';
                
    $string preg_replace($rep'$1 $2_-_-$4'$string); 
            } 
            if (
    preg_match('/([^>]*) (.*)(=\'.*\'|=".*")(.*)/i'$string) > 0) {
                
    $string preg_replace('/([^>]*) (.*)(=\'.*\'|=".*")(.*)/i''$1$4'$string);
            }
            
    $rep '/([^>]*) (' $allowedAttributes ')(_-_-)(\'.*\'|".*")/i';
            if (
    $allowedAttributes) {
                
    $string preg_replace($rep'$1 $2=$4'$string);
            }
            return 
    strip_tags($string$allowedTags);
        } 

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The strip_tags call accepts a parameter telling it specifically what to strip so it can do what you want.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't rely on strip_atgs, it has a lot of security holes. I'd suggest that you take a look at HtmlPurifier

  5. #5
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Don't rely on strip_atgs, it has a lot of security holes. I'd suggest that you take a look at HtmlPurifier
    HTMLPurifier filters HTML, id doesn't remove tags unless they are not valid in the doctype. Img tags inside a text is valid makrup so HTMLPurifier would not help him.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    HTMLPurifier filters HTML, id doesn't remove tags unless they are not valid in the doctype. Img tags inside a text is valid makrup so HTMLPurifier would not help him.
    Which can also remove tags even valid tags. Do you not read the documentation?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Which can also remove tags even valid tags. Do you not read the documentation?


    I never used it for removing HTML tags. I learn something new every day.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Purifier doesn't solve his issue in the right manner.

    Just use a regex.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •