SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: unstrip_tags?

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unstrip_tags?

    I have some text that runs through htmlspecialchars but then I need to allow HTML tags. A reverse strip_tags function would do exactly what I need, but obviously that doesn't exist. What's the best way to go about this?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cant you just have 2 separate variables? one that you run through htmlspecialchars and one that has all the tags intact?

  3. #3
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about htmlentities and html_entity_decode?

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Jasper. - I'm not sure what you mean?

    @devbanana - that's no use as I need to keep the characters that have been converted to entities as entities.

    Maybe I've not explained it clearly enough... I want to convert all instances of ampersands, single quotes, double quotes, less thans and greater thans (which is why I'm using htmlspecialchars), except for when they're html tags such as <img, <a href etc (which is why I want an unstrip_tags).

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    htmlentities and html_entity_decode are going to give too many ampersands in the wrong place. I think I get what you want to do, and it seems that the only way you'll be able to do it is with a regex. I'm not a pro with regex like some people seem to be on here, but I'd be using one for each of the chars I wanted to replace, and you'll be needing negative lookarounds too. http://www.regular-expressions.info/ is a good place to start looking

  6. #6
    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)
    Quote Originally Posted by brangy View Post
    I have some text that runs through htmlspecialchars but then I need to allow HTML tags.
    What exactly are you trying to achieve? If you want to allow HTML, then don't run it through htmlspecialchars. That functions is basically for displaying HTML as text, in a HTML context.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using htmlspecialchars because I want to convert the characters I mentioned above into their entities so that the page validates. Doing that stops HTML tags from working, though.

  8. #8
    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)
    Quote Originally Posted by brangy View Post
    I'm using htmlspecialchars because I want to convert the characters I mentioned above into their entities so that the page validates. Doing that stops HTML tags from working, though.
    So you're trying to turn malformed HTML into HTML? You can use html-tidy for that. There's an extension in PECL, or you can use the binary over the command line.


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
  •