Skip to content

Instantly share code, notes, and snippets.

@skrajewski
Last active April 9, 2021 11:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrajewski/81b02879a8dfec79e12d to your computer and use it in GitHub Desktop.
Save skrajewski/81b02879a8dfec79e12d to your computer and use it in GitHub Desktop.
Replace non-breaking spaces with normal spaces
/**
* Replace non-breaking spaces with normal spaces
*
* @param $str
* @return string
*/
public function removeNbsp($str)
{
$str = htmlentities($str);
$str = str_replace(" ", " ", $str);
$str = html_entity_decode($str);
return $str;
}
@davidglezz
Copy link

$regular_spaces = str_replace("\xc2\xa0", ' ', $original_string);

@nirmit
Copy link

nirmit commented May 15, 2020

$regular_spaces = str_replace("\xc2\xa0", ' ', $original_string);

This breaks Unicode characters..

@Zodiac1978
Copy link

Zodiac1978 commented Apr 9, 2021

This breaks Unicode characters..

@nirmit Why should this break unicode characters? Can you please elaborate on this why you think it does break them. Thank you!

(Looks like it is a upvoted and accepted answer here: https://stackoverflow.com/questions/40724543/how-to-replace-decoded-non-breakable-space-nbsp)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment