Page 1 of 1

regexp letters+spaces

Posted: Mon Jul 02, 2007 8:29 am
by Qlubbie
Ok i want to check an input field on letters and spaces and a space must be followed by a letter...
^([a-z]+)$ checks on letters... but what am i supposed to add to allow a space?

Thanx in advance

Posted: Mon Jul 02, 2007 8:35 am
by Gente
Add \s

Posted: Mon Jul 02, 2007 8:35 am
by miro_igov
^[\w]+\s[\w]+

Posted: Mon Jul 02, 2007 8:56 am
by feyd
\w matches far more than simply letters.

Code: Select all

[feyd@home]>php -r "$t = array('last word should pass', 'last word should fail '); foreach($t as $a) { preg_match_all('#^[a-z]+(?:\s[a-z]+)*$#mi', $a, $matches); print_r($matches); }"
Array
(
    [0] => Array
        (
            [0] => last word should pass
        )

)
Array
(
    [0] => Array
        (
        )

)
Is there an actual problem with spaces though? trim() should probably be used anyways..