Strange behavior in a Regex
I was replacing blank lines for a <p> tag.
The regex replace all blacklines allowing white-spaces (\s), for one <p> tag.
For example this string:
$string="with.\n\n\n\nTherefore";
But return 2 <p> tags.
So, i've done this test:(It's not for replace
, just for test)
$string="with.\n\n\n\nTherefore";
$string=preg_replace('/(^)(\s*)($)/m','[$1]|$2|($3)',$string);
echo $string;
And check what's return:
with.
[]|
|()[]||()
Therefore
Imagining:
with.\n
^\n
\n
$^\n$\n
Therefore
The regex add one \n, and the 4th one does not do what 'she' have to do.(jump to the another line).
Someone that can h开发者_C百科elp. basically explain rather than solve the problem. Thanks evryone.
Your regex should match at least one whitespace character. So replace \s* with \s+ or if it needs escaping \s\+
\s* will match every single character, that's because it matches any(*) whitespace(\s), and because of any it includes none. By none I mean that in the string 'abc' \s* will match the "empty" characters between '^' and 'a', 'a' and 'b', 'b' and 'c', 'c' and '$'.
It is really easy to test on a linux terminal like this:
$ echo "abc" | sed 's:\s*:\n:g' # replace \s* with \n for the whole string
a
b
c
$ # ^ the result
As you can see it matches every "empty" character and replaces it with \n
On the other hand \s+ will force the regex to match at least 1(+) whitespace(\s) character, so it works as excpected.
加载中,请稍侯......
精彩评论