By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Ignore or remove whitespace in a string

P: n/a
Which is the simplest way to remove all whitespace from a string? Is there a
simpler method than a regex replace?
Or how can I tell a regex pattern to ignore all whitespace in my subject
string? There is a global modifier to ignore all spaces in the pattern, but
I couldn't find one for ignoring spaces in the subject string. Do I really
have to either do a preg_replace to remove all whitespace or stick a lot of
\s* into my search pattern?

Greetings,
Thomas
Jul 16 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Thomas Mlynarczyk wrote:
Which is the simplest way to remove all whitespace from a string? Is there
a simpler method than a regex replace?
Or how can I tell a regex pattern to ignore all whitespace in my subject
string? There is a global modifier to ignore all spaces in the pattern,
but I couldn't find one for ignoring spaces in the subject string. Do I
really have to either do a preg_replace to remove all whitespace or stick
a lot of \s* into my search pattern?

Greetings,
Thomas


PS....

$nospaces = str_replace(' ', '', $input);

will strip spaces; if you want to take out carriage returns, newlines, etc
you'll need
str_replace("\n", ''
str_replace("\r", ''

etc
Jul 16 '05 #2

P: n/a
In article <bg*************@news.t-online.com>,
"Thomas Mlynarczyk" <bl*************@hotmail.com> wrote:
Which is the simplest way to remove all whitespace from a string?
*All whitespace*, not just space character?

$after=preg_replace('/\s+/','',$before);

(Or the ereg equivalent would be [[:space:]].)
Is there a simpler method than a regex replace?
Assuming the above, and that your PHP does have PCRE support compiled in,
not really. Any particular reason why want to avoid a regex? PCRE is very
fast and (in this case) very simple.
Or how can I tell a regex pattern to ignore all whitespace in my subject
string?
Well, it's possible to tell PCRE to "find X except where it comes
before/after Y", if that's what you mean... It would help if you
elaborated on what specifically you want to accomplish.
There is a global modifier to ignore all spaces in the pattern, but I
couldn't find one for ignoring spaces in the subject string. Do I really
have to either do a preg_replace to remove all whitespace or stick a lot
of \s* into my search pattern?


It depends on what you need to match. Sometimes when PCRE newbies are
tempted to use a lot of \s* they're attempting to workarounds problems for
which PCRE already has more graceful solutions. For instance, ungreedy
matching is often what they need. Or word boundary matching. Or just a
carefully-placed ".*". Etc. If you explain what you want to do, there
may well be non-kludge solution which we can suggest to you.

--
CC
Jul 16 '05 #3

P: n/a
Also sprach CC Zona:
Which is the simplest way to remove all whitespace from a string?
*All whitespace*, not just space character?
$after=preg_replace('/\s+/','',$before);
Would it make a difference if it was just the space character? (I mean would
there be a non-regex solution in that case?)
Is there a simpler method than a regex replace? Assuming the above, and that your PHP does have PCRE support compiled
in, not really. Any particular reason why want to avoid a regex?
Given the possibilities of regex's, they probably use up a lot of either
system ressources or processing time (or both), so one should not use them
when there are other possible solutions.
PCRE is very fast and (in this case) very simple.


Does this mean system ressources and processing time are not an issue here?
Or how can I tell a regex pattern to ignore all whitespace in my
subject string?


Well, it's possible to tell PCRE to "find X except where it comes
before/after Y", if that's what you mean... It would help if you
elaborated on what specifically you want to accomplish.


Basically, I want to parse a string and for the sake of better readability I
want to allow whitespace, just like "$var=1;" and " $var = 1; ". But for
parsing it's easier when there is no whitespace or when the parsing regex
can just ignore it.
Jul 16 '05 #4

P: n/a
Also sprach matty:
Which is the simplest way to remove all whitespace from a string? Is
there a simpler method than a regex replace?
Or how can I tell a regex pattern to ignore all whitespace in my
subject string? There is a global modifier to ignore all spaces in
the pattern, but I couldn't find one for ignoring spaces in the
subject string. Do I really have to either do a preg_replace to
remove all whitespace or stick a lot of \s* into my search pattern?
Depends what you want to match/replace - what are you doing with it?


The whitespace is just there for better readability, but should have no
impact whatsoever on the parsing of the string as it doesn't have any
"meaning".

Jul 16 '05 #5

P: n/a
Also sprach matty:
$nospaces = str_replace(' ', '', $input);

will strip spaces; if you want to take out carriage returns,
newlines, etc you'll need
str_replace("\n", ''
str_replace("\r", ''


Thanks for this hint, but I was hoping there could be something like trim(),
but working on spaces in the middle of the string as well. So str_replace()
is already the most simple thing to do in my case?

Jul 16 '05 #6

P: n/a
Thomas Mlynarczyk wrote:
Also sprach CC Zona:
Which is the simplest way to remove all whitespace from a string?
*All whitespace*, not just space character?
$after=preg_replace('/\s+/','',$before);


Would it make a difference if it was just the space character? (I mean
would there be a non-regex solution in that case?)
Is there a simpler method than a regex replace?
Assuming the above, and that your PHP does have PCRE support compiled
in, not really. Any particular reason why want to avoid a regex?


Given the possibilities of regex's, they probably use up a lot of either
system ressources or processing time (or both), so one should not use them
when there are other possible solutions.
PCRE is very fast and (in this case) very simple.


Does this mean system ressources and processing time are not an issue
here?
Or how can I tell a regex pattern to ignore all whitespace in my
subject string?


Well, it's possible to tell PCRE to "find X except where it comes
before/after Y", if that's what you mean... It would help if you
elaborated on what specifically you want to accomplish.


Basically, I want to parse a string and for the sake of better readability
I
want to allow whitespace, just like "$var=1;" and " $var = 1; ". But for
parsing it's easier when there is no whitespace or when the parsing regex
can just ignore it.

preg_match_all('/(\$[^ =]+)\s*\=\s*([^;]+);/', $input, $matches); should do
what you want; the pcre stuff is pretty good, and if you'd be doing a str_replace
first, you're better off just matching the values out

--
Matt Mitchell - AskMeNoQuestions
Dynamic Website Development and Marketing
Jul 16 '05 #7

P: n/a
In article <bg*************@news.t-online.com>,
"Thomas Mlynarczyk" <bl*************@hotmail.com> wrote:
*All whitespace*, not just space character?
$after=preg_replace('/\s+/','',$before);
Would it make a difference if it was just the space character? (I mean would
there be a non-regex solution in that case?)


str_replace. If you really are that adamently opposed to using regex, you
certainly could use a series of str_replace operations to remove all
whitespace characters. (But since processing time

to be your concern, benchmark it because I suspect that approach would
actually take more time than a single preg_replace.)
PCRE is very fast and (in this case) very simple.


Does this mean system ressources and processing time are not an issue here?


For a single preg_replace of whitespace? That is absolutely trivial. If
that is your reason for avoiding regex, don't bother. For something like
this, it's your programming resources/time that are invaluable: a single
operation is quick to write, easy to read/debug/change, etc. When you're
doing multiple capturing expressions against large arrays, then it's worth
spending some of your time to optimize; for this, no.
Basically, I want to parse a string and for the sake of better readability I
want to allow whitespace, just like "$var=1;" and " $var = 1; ".


For example...?

--
CC
Jul 16 '05 #8

P: n/a
CC Zona:
In article <bg*************@news.t-online.com>,
"Thomas Mlynarczyk" <bl*************@hotmail.com> wrote:
> *All whitespace*, not just space character?
> $after=preg_replace('/\s+/','',$before);


Would it make a difference if it was just the space character? (I mean
would there be a non-regex solution in that case?)


str_replace. If you really are that adamently opposed to using regex, you
certainly could use a series of str_replace operations to remove all
whitespace characters. (But since processing time
to be your concern, benchmark it because I suspect that approach would
actually take more time than a single preg_replace.)


Which is faster I don't know, but you only need one call to str_replace,
e.g.:
str_replace(array("\n", "\r", "\t", " "), '', $str);

André Nęss
Jul 16 '05 #9

P: n/a
Thomas Mlynarczyk:
Also sprach André Nęss:
Which is faster I don't know, but you only need one call to
str_replace, e.g.:
str_replace(array("\n", "\r", "\t", " "), '', $str);


Wow - *that* would work? I didn't know you could pass an array to
str_replace. But would it "perform" significantly better than a set of
several separate str_replace() calls?


No idea, you can always benchmark it though. If the performance difference
between using regex, several str_replace or one str_replace matters to your
application, you probably shouldn't be doing that particular part in PHP
anyway...

In cases where str_replace is sufficient I prefer it. When people see
regular expression it's probably easy to think "Something complicated is
happening here", whereas with str_replace it's very straightforward.

But most scripters are fairly good at regular expressions, so it's not
really a very strong argument.

André Nęss
Jul 16 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.