SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $content = htmlentities($content, ENT_QUOTES) and a strange situation

    Well, data is getting stored in my database as -

    Code:
    <p>Hi there<br />How's life<br />éèçî characters</p>
    Now if I do htmlentities, everything is getting coded, both special charactes like à and stuffs like < and >. Anyone has an idea about how to get rid of this issue?

    Ideally I would like the special characters to be coded, but not the <s and >s so that my html remain as they are.

    Thanks.

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it's just the HTML you want to preserve you'll probably need to convert the < > back from the character entities.

    PHP Code:
    $text str_replace(array('&lt;''&gt;'), array('<''>'), $text); 

    By the way I would recommend doing this, and htmlentities when displaying your data, not when inserting it.
    mikehealy.com.au
    diigital.com art, design . Latest WorkSaturday Morning

  3. #3
    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)
    For a proper solution, you'd have to parse the html. You could probably just use a regular expression, with a callback though:
    PHP Code:
    <?php
    $html 
    "<p>Hi there<br />How's life<br />éèçî characters</p>";
    function 
    callback_replace_htmlentities($matches) {
      return 
    $matches[1].htmlentities($matches[2]);
    }
    echo 
    preg_replace_callback('~(<[^>]+>)([^<]+)~''callback_replace_htmlentities'$html);
    Oh, and I agree with cranial-bore; You shouldn't save data as HTML.

  4. #4
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks Cranial. Yeah the solution was so obvious still I could not figure it out. He he !!!

    Thanks a lot man. I am laughing at myself.

  5. #5
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi kyberfabrikken, can you please tell me what's the advantage of this preg replace solution over the previous one. I am just curious to know.

  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 kigoobe View Post
    Hi kyberfabrikken, can you please tell me what's the advantage of this preg replace solution over the previous one. I am just curious to know.
    None really. I started typing it before cranial-bore hit submit, so I didn't see he already posted a reply.

  7. #7
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, ok ... thanks for clarification.


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
  •