Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- need help with RegEX
- Super Mango
January 18, 2006, 6:51 am
rate this thread
I want to change a given string in a way that:
1) all invisible chars (like ALT+0254) will be changed to spaces
2) all sequence of several whitespaces will be changed to a single
3) leading and trailing spaces will be canceled (I know how to do that
with trim, so this is only nice-to-have)
I cant limit the string to a-zA-Z0-9 because that every visible char is
OK (it's a multi lingual site).
Thanks in advance!
Re: need help with RegEX
Unless you want to jump through hoops, you'll probably want not one, but
two regular expressions here. The first expression takes care of your
first two requests; the second does the trimming.
// Replace multiple white-space character with a single space.
$out = preg_replace('/\s+/', ' ', $in);
// Trim leading and trailing space.
$out = preg_replace('/(^ )|( $)/', '', $out);
Now, "\s" in a preg_replace only matches the common whitespace characters
(space, formfeed, newline, carriage return, horizontal tab, and vertical
Unicode does have plenty of other space-like characters, so you may want
to explicity add them to the first expression, like:
and so on. A good list of Unicode space-like characters can be found here:
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact