Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
phpScott
- DevNet Resident
- Posts: 1206
- Joined: Wed Oct 09, 2002 6:51 pm
- Location: Keele, U.K.
Post
by phpScott »
I am trying to just remove spaces from a string I though
Code: Select all
$string = "some text here";
$path = preg_replace('/\s\s+/', ' ', $string);
would take care of it but alas no.
is still get
Code: Select all
echo $string;
//some text here
//when i would like
//sometexthere
if one of you regex whiz's could help that would be super-smooth.
thanks
-
Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Post
by Chris Corbyn »
You should be aware that \s takes out ALL whitespace (including carriage returns).
But anyhow
Code: Select all
$string = "some text here";
$space_free = preg_replace('/\s+/', '', $string);
//sometexthere
If don't want to take out carriage returns and tabs then don't take the regex route.... a simple str_replace() would make more sense then
EDIT | I see you echo'd $string in your version (that's the original string).
-
phpScott
- DevNet Resident
- Posts: 1206
- Joined: Wed Oct 09, 2002 6:51 pm
- Location: Keele, U.K.
Post
by phpScott »
thanks, know i know why your incharge of the regex forumns.
dope. that was a cut paste thing that I forgot to change to make right.
-
Skara
- Forum Regular
- Posts: 703
- Joined: Sat Mar 12, 2005 7:13 pm
- Location: US
Post
by Skara »
.. there's really no need for a regex here, though.
Code: Select all
$string = str_replace(' ','',$string);
should work fine.