tompat — 2012-12-08T19:53:16-05:00 — #1
I am trying to learn regular expressions and have a question for the following codes
Why does this code allow everything
return (preg_match_all ("/[a-z]\'\-\s*/", $testString));
Why does this allow nothing
return (preg_match_all ("/^[a-z]\'\-\s*$/", $testString));
cpradio — 2012-12-08T20:59:29-05:00 — #2
What test strings are you using?
The difference between the two is quite simple, the latter requires the beginning of the string to match a-z (lowercase) and end with at least 1 space.
tompat — 2012-12-08T21:51:04-05:00 — #3
Then what do I do to make the 1st code only allow whats listed?
cpradio — 2012-12-08T22:06:01-05:00 — #4
Tell me what strings you want captured and what you do not want captured and I can answer that.
tompat — 2012-12-09T01:12:38-05:00 — #5
That is for first and last name, I want to allow for spaces, hyphens and apostrophes. Maybe you clarify my assumptions about regular expressions. Does it return 0 when a character is inserted that is not in the argument or does it just check to see if the listed characters are there?
scallioxtx — 2012-12-09T04:12:34-05:00 — #6
There, fixed that for you
scallioxtx — 2012-12-09T04:14:22-05:00 — #7
I think what you're looking for this
Please note that this does not allow for special characters like the é in my name.
For names I mostly find it easiest to just strip_tags to prevent script injection and be done with it.