tags:

views:

44

answers:

4

I try to delete more then one white characters from my string:

$content = preg_replace('/\s+/', " ", $content); //in some cases it doesn't work

but when i wrote

$content = preg_replace('/\s\s+/', " ", $content); //works fine

could somebody explain why?

because when i write /\s+/ it must match all with one or more white character, why it doesn't work?

Thanks

+4  A: 

What is the minimum number of whitespace characters you want to match?

\s+ is equivalent to \s\s* -- one mandatory whitespace character followed by any number more of them.

\s\s+ is equivalent to \s\s\s* -- two mandatory whitespace characters followed by any number more (if this is what you want, it might be clearer as \s{2,}).

Also note that $content = preg_replace('/\s+/', " ", $content); will replace any single spaces in $content with a single space. In other words, if your string only contains single spaces, the result will be no change.

Daniel Vandersluis
minimum number is 2, but i don't see any potential risk of using `/\s+/` structure, but in some cases it deletes a part of string. i use this structure in myslq database, and in some fields i met such problem.
Syom
`/\s+/` doesn't match enforce the 2 space requirement, as mentioned.
Daniel Vandersluis
ok, but how can it delete other words, i can't understand anyway.
Syom
Edit your question with an example of what problem you are experiencing and we'll be able to help better.
Daniel Vandersluis
@Daniel, why will preg_replace('/\s+/', ...) only replace *single* spaces? The docs say that preg_replace is greedy by default.
LarsH
@LarsH: they are indeed greedy by default. `/\s+/` will not **only** replace single spaces, but `\s+` **will** match a single space on its own (whereas `\s\s+` will not). I'm not sure what the distinction you're trying to make is, though?
Daniel Vandersluis
@Daniel - OK, I see now what you're saying. Your last edit clarified this. However I don't think this addresses his problem. He seems to want to collapse multiple consecutive spaces to single spaces (IIUC). If the initial $content contains only single spaces, the output would be correct, and he would not be posting that it doesn't work. Oh never mind, the problem is somewhere else.
LarsH
@LarsH, yeah that note was more as an afterthought than anything.
Daniel Vandersluis
A: 

I just wanted to add to that the reason why your /s+/ worked sometimes and not others, is that regular expressions are very greedy, so it is going to try to match one or more space characters, and as many as it can match. I think that is where you got tripped up in finding a solution.

Sorry I'm not yet able to add comments, or I would have just added this comment to Daniel's answer, which is good.

Christa
I think this would have the opposite effect. A greedy regexp would match as many spaces as possible and replace them with a single space, thus accomplishing what the OP wanted. An _ungreedy_ regexp would cause the problem behavior. Right?
LarsH
A: 

Are you using the Ungreedy option (/U)? It doesn't say so in your code, but if so, it would explain why the first preg_replace() is replacing each single space with a single space (no change). In that case, the second preg_replace() would be replacing each double space with a single space. If you try the second one on a string of four spaces and the result is a double space, I would suspect ungreediness.

LarsH
A: 

try preg_replace("/([\s]{2,})/", " ", $text)

Quamis
there's no need to stick `\s` in a character class, or to stick the whole expression in a group. `/\s{2,}/` is equivalent, shorter and clearer.
Daniel Vandersluis
i think they look clearer when everything is in its group or some other type of separation. But yes, they are equivalent.
Quamis