/    Sign up×
Community /Pin to ProfileBookmark

preg_match function

hi guys:

could anyone tell me what is the

preg_match(“([0-9]{4})”,$templatefile,$matches);

mean? thank you

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 02.2005 — It should be:
[code=php]
preg_match("/([0-9]{4})/",$templatefile,$matches);
[/code]

The first parameter is the pattern to search for.
[list]
  • [*]The '/' characters surround the entire search pattern (there are optional regexp arguments which can follow the end '/')

  • [*]The parentheses (probably unnecessary in this example) are grouping everything inside of them, and if it matches, the part of the string matched will be stored in variable $1 (each successive pair of parentheses being saved as $2, $3, etc.)

  • [*][0-9] means match any number from 0 to 9

  • [*]{4} means the preceding character must repeat for an exact total of 4 times
  • [/list]

    The second parameter is what is to be searched.

    The third parameter (optional) is an array which will store all matches found.

    So, your preg_match is saying to search $templatefile for each sequence of 4 numbers from 0-9, and save any matches found in array $matches.
    Copy linkTweet thisAlerts:
    @handong888authorFeb 03.2005 — thank you very much and you are so helpful
    Copy linkTweet thisAlerts:
    @NogDogFeb 03.2005 — The regular expression (or "regexp") syntax is essentially a mini, special-purpose programming language of its own. The PHP documentation for the preg (Perl-style regexp) syntax is here: http://us4.php.net/manual/en/reference.pcre.pattern.syntax.php

    As it is based on Perl regexp's, you can also find useful information in many Perl books and web sites.
    Copy linkTweet thisAlerts:
    @ShrineDesignsFeb 03.2005 — also JavaScript uses a similar regexp syntax
    ×

    Success!

    Help @handong888 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...