★ wanayoo — archive 1999 http://php.net/manual/function.preg-match.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to PCRE
Quick Reference
PCRE
German version of this pageJapanese version of this page
Italian version of this pageFrench version of this pageHungarian version of this pageSpanish version of this page
* preg_match
* preg_match_all
* preg_replace
* preg_split
* preg_quote
* preg_grep
* Pattern Modifiers
* Pattern Syntax
Manual: preg_match
View the source code for this pageSearch the site



Previous page
 PCRE
 Updated
Wed, 23 Aug 2000
preg_match_all 
Next page


preg_match

(PHP3 >= 3.0.9, PHP4 )

preg_match -- Perform a regular expression match

Description

int preg_match (string pattern, string subject [, array matches])

Searches subject for a match to the regular expression given in pattern.

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that match the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

Returns true if a match for pattern was found in the subject string, or false if not match was found or an error occurred.

Example 1. find the string of text "php"


// the "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}
      

Example 2. find the word "web"


// the \b in the pattern indicates a word boundary, so only the distinct
// word "web" is matched, and not a word partial like "webbing" or "cobweb"
if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}
if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
    print "A match was found.";
} else {
    print "A match was not found.";
}
      

Example 3. Getting the domain name out of a URL


preg_match("/^(.*)([^\.]+\.[^\.]+)(\/.*)?/U",
           "http://www.php.net/index.html", $matches);
// show the second parenthesized subpattern
echo "domain name is: ".$matches[2]."\n"; 
      
This example will produce:

domain name is: php.net
     
See also preg_match_all(), preg_replace(), and preg_split().


User Contributed Notes: preg_match


zot@zotconsulting.com
28-Dec-1999 02:55
not supplying the match array will cause php to hang.


indyj@yahoo.com
02-Mar-2000 01:42
RTFM folks ... none of the PCRE functions are in PHP unless *you* compile PHP with the --with-pcre-regex flag.

So don't bad-mouth PHP because you can't read. :)



chrisbolt@home.com
12-Mar-2000 02:23
If you want to use some of PHP's special characters in your expression, such as $, you must escape it twice. For example:
<PRE>$matchme = "\$example";
if (preg_match("/\$example/", $matchme)) {</PRE>
will not be matched because PHP interprets the \$ and passes it as $. Instead, you must do this:
<PRE>if (preg_match("/\\\$example/", $matchme)) {</PRE>



flec@flec.co.uk
24-Jul-2000 02:42
To mike@radiotakeover.com and anyone else:

Read http://www.perl.com/pub/doc/manual/html/pod/perlre.html

for PCRE documentation.



 About Notes


Previous page
 PCRE
 Updated
Wed, 23 Aug 2000
preg_match_all 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.