vigour;10964793 wrote:
I want to check if there are 2 or more spaces in a row, how do I modify my code so it wont allow that? Or even better, take away all but 1 space automatically?
Use this to replace multiple whitespaces with a single one in each place. But do note that \s matches any whitespace character, such as new line, horizontal tab etc. Thus
$string = "A B
C";
$string = preg_replace('/(\s)+/', '\1', $string);
Gives
A B
C
If you only want to allow the ordinary space character, then use that instead of \s
^/[A-Za-z0-9 ]$/
And the replacement would no longer need a back reference
$string = preg_replace('/ +/', ' ', $string);