Simple demo of preg_match and preg_match_all

matching Array ( [0] => /matches/ [1] => /catch/ [2] => /atch/ [3] => /matches/i )
in : What matches here! Matches there? We don't need no matches.


MATCH: /matches/ was found: Array ( [0] => Array ( [0] => matches [1] => 5 ) )
MATCH ALL: /matches/ was found: Array ( [0] => Array ( [0] => matches [1] => matches ) )
MATCH: /catch/ not found
MATCH ALL: /catch/ not found
MATCH: /atch/ was found: Array ( [0] => Array ( [0] => atch [1] => 6 ) )
MATCH ALL: /atch/ was found: Array ( [0] => Array ( [0] => atch [1] => atch [2] => atch ) )
MATCH: /matches/i was found: Array ( [0] => Array ( [0] => matches [1] => 5 ) )
MATCH ALL: /matches/i was found: Array ( [0] => Array ( [0] => matches [1] => Matches [2] => matches ) )

Simple character classes

matching /[aeiou][1234567890][a-z]/
in: ae12abc r2d2 a1steaksauce


MATCH: /[aeiou][1234567890][a-z]/ was found: Array ( [0] => a1s )

Using repetition together with character classes

matching /[aeiou]+[1234567890]*[a-z]?/
in: ae12abc r2d2 a1steaksauce


MATCH ALL: /[aeiou]+[1234567890]*[a-z]?/ was found: Array ( [0] => Array ( [0] => ae12a [1] => a1s [2] => eak [3] => auc [4] => e ) )

matching /\d+\.\d{5}/
in: 3 .14159 3.1415 3.14159


MATCH ALL: /\d+\.\d{5}/ was found: Array ( [0] => Array ( [0] => 3.14159 ) )

Using submatches as part of an overall match.

matching: Array ( [0] => /(\w+)\1/ [1] => /(\w+)(.*)(\1)/ [2] => /(\w+)\1\b/ )
in: Array ( [0] => catcat [1] => abc123abc [2] => matchmatchhere )

MATCH: /(\w+)\1/ matches Array ( [0] => catcat [1] => cat )
MATCH: /(\w+)\1/ matches Array ( [0] => matchmatch [1] => match )
MATCH: /(\w+)(.*)(\1)/ matches Array ( [0] => catcat [1] => cat [2] => [3] => cat )
MATCH: /(\w+)(.*)(\1)/ matches Array ( [0] => abc123abc [1] => abc [2] => 123 [3] => abc )
MATCH: /(\w+)(.*)(\1)/ matches Array ( [0] => matchmatch [1] => match [2] => [3] => match )
MATCH: /(\w+)\1\b/ matches Array ( [0] => catcat [1] => cat )

Simple demo of preg_replace

replacing :Array ( [0] => /(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/ [1] => /^\s*{(\w+)}\s*=/ )
with: Array ( [0] => \3/\4/\1\2 [1] => $\1 = )
in : {mydate} = 1995-3-24

$mydate = 3/24/1995