Php: Want to learn PCRE Syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
montyauto
Forum Commoner
Posts: 25
Joined: Sat Jan 13, 2007 7:05 am

Php: Want to learn PCRE Syntax

Post by montyauto »

I've been learning the php function which uses a Perl-compatible regular expression syntax like preg_match(), ereg() etc.

The function itself is not difficult to understand but when coming to the pattern syntax like Meta-characters, Pattern Modifiers quite make me headache.

See the example below:

Code: Select all

preg_match_all("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x",
                "Call 555-1212 or 1-800-555-1212", $phones);
or

Code: Select all

if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
    echo "$regs[3].$regs[2].$regs[1]";
or

Code: Select all

preg_match_all("|<[^>]+>(.*)</[^>]+>|U", 
    "<b>example: </b><div align=left>this is a test</div>", 
    $out, PREG_PATTERN_ORDER);

Is there any resources other than the online php manual providing step by step level preferably with a lot of example to understand better about how to build this pattern from the scratch?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Post Reply