1

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!

Roman Holzner
  • 5,738
  • 2
  • 21
  • 32
  • 3
    Your code is basically right, you just used the wrong kind of quotes. Inside double quotes, you need to escape the backslash, `\\s`. – Barmar Mar 04 '13 at 23:09
  • Thank you very much, and sorry for the duplicate! And thanks for the downvote -.- – Roman Holzner Mar 04 '13 at 23:14

1 Answers1

2

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

Green Black
  • 5,037
  • 1
  • 17
  • 29