An odd thing occurred today, I was trying to do trim($string) on a string in php, but I kept getting strings back that appeared to have a space at the beginning, the exact thing that trim is supposed to remove. I carefully checked and the space was definitely found when I copied the result out to TextWrangler. Finally I tried substr($string,1) and then I got the answer, the new string started nbsp; the string I was trimming had , a non-breaking space at the beginning, which trim doesn’t remove. A quick switch to str_replace and now my string doesn’t have a space at the beginning. 🙂
-
Archives
- October 2023
- June 2022
- June 2020
- December 2019
- July 2019
- June 2019
- May 2019
- October 2018
- April 2018
- March 2018
- December 2017
- May 2017
- October 2016
- August 2016
- June 2016
- May 2016
- November 2015
- October 2015
- June 2015
- March 2015
- November 2014
- October 2014
- May 2014
- January 2014
- September 2013
- August 2013
- July 2013
- April 2013
- December 2012
- November 2012
- August 2012
- June 2012
- May 2012
- March 2012
- January 2012
- December 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- September 2009
- June 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- June 2008
- May 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- March 2006
- February 2006
- December 2005
- November 2005
- October 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
-
Meta
I just had a similar issue. My page was encoded in UTF-8, in which case the encoding for a non-breaking space is 0xC2 0xA0, so I had to use this code to get rid of them (I also had to get rid of carriage return characters, hence the “\n”):
$string = trim( $string, “\xC2\xA0\n” );
Hi,
danke. Hatte das gleiche Problem. Ich hatte mit
$str = trim(preg_replace('/\s+/', ' ', $str));
alle Whitespaces entfernt. Dachte ich zumindest. Es waren noch mehrere Leerzeichen vorhanden.
Mit ord() resultierte es das ASCII Zeichen 160. Das verwirrte mich, weil das ja eigentlich das “á” ist. Erst nach einer weiteren Recherche kam ich auf das non braking space und dann auch auf diesen Blog, der mir dann die Lösung brachte (ISO):
$str = str_replace("\xA0", " ", $str);
Thank you very much Bryan. Your solution was perfect. I couldn’t figure out why my parsed Excel spreadsheet rows were coming out with leading spaces, even after trim, preg_replace, and ltrim. Finally, I converted the first character to a HEX, which resulted in ‘a0’ then found that was a non-breaking space, and here we are.
Replace nbsp (non-breaking space) with regular space like so.
$value = preg_replace('~\x{00a0}~siu',' ',$value);
Note that there is a space between the two single quotes after “siu’,” and before “,$value”. You could change that to ” (no space) to remove the nbsp.
// — quick solution to remove utf-8 etc. (urlencode) —
array_walk_recursive($data, function (&$item)
{
$item = trim(preg_replace(‘/[\x00-\x1F\x7F\xA0]/u’, “”, $item));
});