This question already has an answer here:
I have a Problem with RegEx and PHP, I have this string:
Panorama - SPIEGEL ONLINE - Nachrichten Schlagzeilen Hilfe RSS Newsletter Mobil Wetter TV-Programm Dienstag, 26. Februar 2013 Panorama NACHRICHTEN Home Politik Deutschland Ausland WirtschaftB
I would like to strip all spaces, which are not needed, so all spaces but one, so that the words are still seperated by one space.
I wrote this regex:
echo trim(preg_replace("/\s+/", " ", $lol));
And I'm very close:
Panorama - SPIEGEL ONLINE - Nachrichten Schlagzeilen Hilfe RSS Newsletter Mobil Wetter TV-Programm Dienstag, 26. Februar 2013 Panorama NACHRICHTEN Home Politik Deutschland Ausland WirtschaftB
What am I missing? Thank you very much!
</div>
The problem is the
. This is a non-breakable space in your browser. Use this:
$string = "Panorama - SPIEGEL ONLINE - Nachrichten Schlagzeilen Hilfe RSS Newsletter Mobil Wetter TV-Programm Dienstag, 26. Februar 2013 Panorama NACHRICHTEN Home Politik Deutschland Ausland WirtschaftB";
$string = str_replace(" "," ",$string);
echo preg_replace('!\s+!', ' ', $string);
demo: http://sandbox.onlinephpfunctions.com/code/8c85dcc5ba0c9aa9306125ad5878c02d07fcf452