zslash;11027867 wrote:well actually, it was my bad lol. i had already filtered the text i wanted to check ontop of the script... what a noob i am lol xD
Well, notwithstanding:
preg_match('/[^A-Za-z ]/',$name) //i only want to allow a-z and spaces.
//ur doing it wrong
bradgrafelman;11027835 wrote:Works as expected for me...
I think what bradgrafelman is pointing out is that your code is producing the correct output (i.e., "as expected" considering what you wrote). However, your comment indicates that you actually want something else: only a-z and spaces.
If I misread that, let me know.
[font=monospace][A-Za-z ][/font] means "match if any characters in the string are not a-z or a space" - that's why "test test" -the only test string that really is "a-z and spaces"- is the only one that fails.
"match if all characters are a-z (case-insensitive) or spaces" looks like this:
[B]/^[ A-Za-z]*$/[/B]
^ [COLOR="#A9A9A9"]// start-of-line anchor (i.e., match from the beginning)[/COLOR]
[ [COLOR="#A9A9A9"]// begin character class[/COLOR]
[COLOR="#A9A9A9"]// (<-- that's a space over there)[/COLOR]
A-Z [COLOR="#A9A9A9"]// uppercase "A" through "Z"[/COLOR]
a-z [COLOR="#A9A9A9"]// lowercase "a" through "z"[/COLOR]
] [COLOR="#A9A9A9"]// end character class[/COLOR]
* [COLOR="#A9A9A9"]// zero or more of those characters[/COLOR]
$ [COLOR="#A9A9A9"]// end-of-line anchor (i.e., match until the end)
// using ^ and $ means you match against the entire string.
// therefore, you're looking for *only* those characters, instead of those characters *and/or anything else*.
// (or, use pattern modifiers)[/COLOR]
/^[ a-z]*$/i [COLOR="#A9A9A9"]// "i" means "case-insensitive."[/COLOR]