Does html_entity_decode replaces   also? If not how to replace it?

Abhishek Sanghvi picture Abhishek Sanghvi · Jun 8, 2011 · Viewed 27.9k times · Source

I have a situation where I am passing a string to a function. I want to convert   to " " (a blank space) before passing it to function. Does html_entity_decode does it?

If not how to do it?

I am aware of str_replace but is there any other way out?

Answer

Salman A picture Salman A · Jun 8, 2011

Quote from html_entity_decode() manual:

You might wonder why trim(html_entity_decode(' ')); doesn't reduce the string to an empty string, that's because the ' ' entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 characterset.

You can use str_replace() to replace the ascii character #160 to a space:

<?php
$a = html_entity_decode('>&nbsp;<');
echo 'before ' . $a . PHP_EOL;
$a = str_replace("\xA0", ' ', $a);
echo ' after ' . $a . PHP_EOL;