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$contentwith a single space. In other words, if your string only contains single spaces, the result will be no change.