开发者

regex: delete white characters

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


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜