Special Characters to Use in PHP Expression Test Patterns

By Janet Valade

Part of PHP & MySQL For Dummies Cheat Sheet

Regular PHP expressions use patterns to test whether the input users submit when using online forms are in the correct format. This table shows characters you can use in patterns.

Character Meaning Example Match Not a Match
^ Beginning of line ^c cat my cat
$ End of line c$ tic stick
. Any single character .. me, go a
? Preceding item is optional mea?n mean, men moan
( ) Groups literal characters m(ea)n mean men,mn
[ ] Any character in set abc[1-3] abc1,abc2 abc4
[! ] Any character not in set m[!ea]n min, mon men, man
+ One or more door[1-3]+ door111, door131 door, door55
* Zero or more door[1-3]* door, door311 door4, door445
{ , } Range of repetitions a{2,5} aa,aaaaa a, xx3
Escapes character m*n m*n men, mean
( | | ) Alternate strings (Tom|Tommy) Tom, Tommy Thomas, To