Wednesday, December 9, 2009

PHP trim/strip non-breaking space

I was just writing some PHP and having the hardest time getting rid of a non-breaking space ( ) character in some HTML I was parsing.  I finally figured it out:  the page was encoded in UTF-8, and the encoding for a non-breaking space is 0xC2 0xA0.  This is the code that finally did the trick (I also had to get rid of carriage return characters, hence the “\n”):

$string = trim( $string, "\xC2\xA0\n" );

3 comments:

  1. Where would I put this code? Functions.php / header.php?

    Thanks

    ReplyDelete
  2. I'm not sure what the context is. Functions.php/header.php could be referring to anything. Are you talking about wordpress?

    At any rate, the code that I shared is general enough that it could go anywhere you need to trim a non-breaking space from a string. You'll have to figure out where in your code it needs to go. If you have specific programming questions I recommend http://stackoverflow.com/

    ReplyDelete
  3. Thanx, man! very helpful advice!

    ReplyDelete