+ Reply to Thread
Results 1 to 8 of 8

Thread: php htmlentities()

  1. #1
    taekwondokid42 is offline x10 Lieutenant taekwondokid42 is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    268

    php htmlentities()

    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. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: php htmlentities()

    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 by garrettroyce; 02-23-2009 at 07:11 PM. Reason: forgot [/code]
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: php htmlentities()

    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. #4
    Spasm is offline x10Hosting Member Spasm is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    10

    Re: php htmlentities()

    Quote Originally Posted by freecrm View Post
    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;');
    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 by Spasm; 02-24-2009 at 10:09 AM.

  5. #5
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: php htmlentities()

    Good call, Spasm.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  6. #6
    taekwondokid42 is offline x10 Lieutenant taekwondokid42 is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    268

    Re: php htmlentities()

    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. #7
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: php htmlentities()

    Quote Originally Posted by taekwondokid42 View Post
    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.

    Thank you very much! Spasm posted the correct solution. I hope this works for you!
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  8. #8
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: php htmlentities()

    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!

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers