php htmlentities()

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by taekwondokid42, Feb 23, 2009.

  1. taekwondokid42

    taekwondokid42 New Member

    Messages:
    268
    Likes Received:
    0
    Trophy Points:
    0
    I want to use the htmlentites() method, but I don't want the method to change the input ONLY FOR the tag "<br>", and the phrase "&nbsp;" I will take code alternate to htmlentities(), so long as it will not compromise security.
     
  2. garrettroyce

    garrettroyce Community Support Community Support

    Messages:
    4,761
    Likes Received:
    163
    Trophy Points:
    63
    unfortunately, htmlentities() will not discriminate between types of tags. This should work:

    Code:
    function ReplaceBR_NBSP(input_str)
    {
        return str_ireplace(array('<br>','&nbsp'),array('&lt;br&gt;','&amp;nbsp;'),input_str);
    }
    This works by finding the phrases '<br>' or '&nbsp' (case insensitive) and replacing them with the html encoded equivalents for any string or array of strings input.

    hope this helps! :)
     
    Last edited: Feb 23, 2009
  3. freecrm

    freecrm New Member

    Messages:
    629
    Likes Received:
    0
    Trophy Points:
    0
    I'm intrigued...

    What is the str_ireplace function?

    I would have just used

    str_replace('<br'>, '&lt;br&gt;');
    str_replace('&nbsp;', '&amp;nbsp;');
     
  4. Spasm

    Spasm New Member

    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    str_ireplace is the same as str_replace, except it ignores case (ie "<br>" and "<BR>" would both be replaced). Most string functions have a counterpart that ignores case, that's what the "i" stands for.

    I don't know how experienced you are with PHP, so this explanation may be a little confusing: str_replace can accept arrays for arguments as well as strings. If you give it an array, it will work through each element and replace it like normal. Check the function reference page: http://us2.php.net/manual/en/function.str-ireplace.php . Your code and garret's will do the same thing. I'll rewrite his code so it's easier to visualize:

    Code:
    function ReplaceBR_NBSP($input_str)
    {
    
        $search[0] = '<br>';
        $search[1] = '&nbsp;';
    
        $replace[0] = '&lt;br&gt;';
        $replace[1] = '&amp;nbsp;';
    
    
        return str_ireplace($search, $replace, $input_str);
    
    }
    
    PS - I reread your post, do you want to convert everything to html entities EXCEPT "<br>" and "&nbsp;", or ONLY those two strings, because that function will replace ONLY those two. If you want it the other way around, do $input_str = htmlentities($input_str), then switch $search and $replace. That way, it will convert everything, then revert line breaks and spaces to the way they were before.
     
    Last edited: Feb 24, 2009
  5. garrettroyce

    garrettroyce Community Support Community Support

    Messages:
    4,761
    Likes Received:
    163
    Trophy Points:
    63
  6. taekwondokid42

    taekwondokid42 New Member

    Messages:
    268
    Likes Received:
    0
    Trophy Points:
    0
    I'll give you reputation (first reply) because it was helpful, but I was actually looking for the reverse. I want to replace the '&amp;nbsp;' with '&nbsp;', not the other way around. But I can use the same method.
     
  7. garrettroyce

    garrettroyce Community Support Community Support

    Messages:
    4,761
    Likes Received:
    163
    Trophy Points:
    63

    Thank you very much! Spasm posted the correct solution. I hope this works for you!
     
  8. freecrm

    freecrm New Member

    Messages:
    629
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the help on this. I am not particularly experienced with php but know enought to understand the array point.

    The "i" prefix with be very useful for other projects!
     

Share This Page