PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

preg_quote> <preg_match_all
Last updated: Fri, 02 Jan 2009

view this page in

preg_match

(PHP 4, PHP 5)

preg_matchPerform a regular expression match

Description

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )

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

Parameters

pattern

The pattern to search for, as a string.

subject

The input string.

matches

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

flags

flags can be the following flag:

PREG_OFFSET_CAPTURE
If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at index 0 and its string offset into subject at index 1.

offset

Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).

Note: Using offset is not equivalent to passing substr($subject, $offset) to preg_match() in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). Compare:

<?php
$subject 
"abcdef";
$pattern '/^def/';
preg_match($pattern$subject$matchesPREG_OFFSET_CAPTURE3);
print_r($matches);
?>

The above example will output:

Array
(
)

while this example

<?php
$subject 
"abcdef";
$pattern '/^def/';
preg_match($patternsubstr($subject,3), $matchesPREG_OFFSET_CAPTURE);
print_r($matches);
?>

will produce

Array
(
    [0] => Array
        (
            [0] => def
            [1] => 0
        )

)

Return Values

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred.

Changelog

Version Description
4.3.3 The offset parameter was added
4.3.0 The PREG_OFFSET_CAPTURE flag was added
4.3.0 The flags parameter was added

Examples

Example #1 Find the string of text "php"

<?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.")) {
    echo 
"A match was found.";
} else {
    echo 
"A match was not found.";
}
?>

Example #2 Find the word "web"

<?php
/* 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.")) {
    echo 
"A match was found.";
} else {
    echo 
"A match was not found.";
}

if (
preg_match("/\bweb\b/i""PHP is the website scripting language of choice.")) {
    echo 
"A match was found.";
} else {
    echo 
"A match was not found.";
}
?>

Example #3 Getting the domain name out of a URL

<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
    
"http://www.php.net/index.html"$matches);
$host $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/'$host$matches);
echo 
"domain name is: {$matches[0]}\n";
?>

The above example will output:

domain name is: php.net

Example #4 Using named subpattern

<?php

$str 
'foobar: 2008';

preg_match('/(?<name>\w+): (?<digit>\d+)/'$str$matches);

print_r($matches);

?>

The above example will output:

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

Notes

Tip

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.



preg_quote> <preg_match_all
Last updated: Fri, 02 Jan 2009
 
add a note add a note User Contributed Notes
preg_match
Reno
06-Jan-2009 01:52
I modified your email validation pattern to solve these issues:

- the string MUST contain a TLD
- TLD can be 2 letters long as well as 3 or more (ie: .ca, .us, .uk, .fr, etc.)
- domain name (tld not included) must contain at least 2 characters
- domain name can contain "-"if it's not the first nor the last character.

<?php

$pattern
= '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .
'(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';

echo
preg_match ($pattern, "email-address-to-validate@host.tld");

?>
shamun dot toha at gmail dot com
25-Dec-2008 11:58
The above patterns are tested but for this type of
emails those get fails. This is most valid pattern.
<?php
/**
 * Most corrected pattern for Email validation.
 *
 */

 // Valid email
echo preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*
\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i'
,'09_az..AZ@host.dOMain.cOM');

// Invalid emails             
echo preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*
\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i'
,'09_azAZ@ho...st...........domain.com');
                           
echo
preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*
\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i'
,'09_azAZ@host.do@main.com');                     
?>
----------------------------
Output:
----------------------------
1 = valid
0 = invalid
0 = invalid
Alex Zinchenko
11-Dec-2008 03:15
If you need to check whether string is a serialized representation of variable(sic!) you can use this :

<?php

$string
= "a:0:{}";
if(
preg_match("/(a|O|s|b)\x3a[0-9]*?
((\x3a((\x7b?(.+)\x7d)|(\x22(.+)\x22\x3b)))|(\x3b))/"
, $string))
{
echo
"Serialized.";
}
else
{
echo
"Not serialized.";
}

?>

But don't forget, string in serialized representation could be VERY big,
so match work can be slow, even with fast preg_* functions.
rbotzer at yahoo dot com
01-Dec-2008 08:36
@Ben:

Your pattern will match 1.1.255.299  (it matches the .29 at the end out of subpattern .299)

This pattern eliminates such false positives:
/^((1?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(1?\d{1,2}|2[0-4]\d|25[0-5]){1}$/

Ronen
dbreen at gmail dot com
21-Nov-2008 06:35
When I was using the above example's syntax for named capturing groups, it worked fine on my development server (PHP 5.2.6), but then gave me a regex error on the live server (PHP 5.0.4).

By adding a 'P' in front of the parameter name, it seems to have resolved the issue (this is in accordance w/ the PCRE implementation).

To use the above example, here's the original:
<?php
preg_match
('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
?>

And here's the fix:
<?php
preg_match
('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
?>
Ben
25-Oct-2008 08:47
Marc your pattern will match 259.259.259.259

I think you're actually after something like this:

/((1?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(1?\d{1,2}|2[0-4]\d|25[0-5])/
phil dot taylor at gmail dot com
23-Oct-2008 02:01
If you need to check for .com.br and .com.au and .uk and all the other crazy domain endings i found the following expression works well if you want to validate an email address. Its quite generous in what it will allow

<?php

        $email_address
= "phil.taylor@a_domain.tv";

    if (
preg_match("/^[^@]*@[^@]*\.[^@]*$/", $email_address)) {
        return
"E-mail address";       
    }
       
?>
Jonathan Camenisch
16-Oct-2008 04:21
@ Marc

A little more work to do--your expression matched ...256... through ...259..., and will not match 1- or 2-digit numbers that do not start with 1. It could also be a little more concise, as in:

/^(1?\d{1,2}|2([0-4]\d|5[0-5]))(\.(1?\d{1,2}|2([0-4]\d|5[0-5]))){3}$/

Also, I put together a primitive regex tester at http://j-r.camenisch.net/regex/ -- to help someone find more flaws to correct. ;-)
Marc
06-Oct-2008 10:16
@ Steve Todorov:
Your regex will not only match 999.999... but also 9999.9999... etc.

I'd rather take this regex:

/^(1\d{0,2}|2(\d|[0-5]\d)?)\.(1\d{0,2}|2(\d|[0-5]\d)?)
\.(1\d{0,2}|2(\d|[0-5]\d)?)\.(1\d{0,2}|2(\d|[0-5]\d)?)$/

this should represent any ip (v4). At least it did in a small test here ;)
Steve Todorov
03-Oct-2008 03:23
While I was reading the preg_match documentation I didn't found how to match an IP..
Let's say you need to make a script that is working with ip/host and you want to show the hostname - not the IP.

Well this is the way to go:

<?php
/* This is an ip that is "GET"/"POST" from somewhere */
$ip = $_POST['ipOrHost'];

if(
preg_match('/(\d+).(\d+).(\d+).(\d+)/',$ip))
 
$host = gethostbyaddr($ip);
else
 
$host = gethostbyname($ip);

echo
$host;
?>

This is a really simple script made for beginners !
If you'd like you could add restriction to the numbers.
The code above will accept all kind of numbers and we know that IP address could be MAX 255.255.255.255 and the example accepts to 999.999.999.999.

Wish you luck!

Best wishes,
Steve
Ashus
12-Sep-2008 05:18
If you need to match specific wildcards in IP address, you can use this regexp:

<?php

$ip
= '10.1.66.22';
$cmp = '10.1.??.*';

$cnt = preg_match('/^'
    
.str_replace(
     array(
'\*','\?'),
     array(
'(.*?)','[0-9]'),
    
preg_quote($cmp)).'$/',
    
$ip);

echo
$cnt;

?>

where '?' is exactly one digit and '*' is any number of any characters. $cmp mask can be provided wild by user, $cnt equals (int) 1 on match or 0.
wjaspers4[at]gmail[dot]com
28-Aug-2008 04:55
I found this rather useful for testing mutliple strings when developing a regex pattern.
<?php
/**
 * Runs preg_match on an array of strings and returns a result set.
 * @author wjaspers4[at]gmail[dot]com
 * @param String $expr The expression to match against
 * @param Array $batch The array of strings to test.
 * @return Array
 */
function preg_match_batch( $expr, $batch=array() )
{
// create a placeholder for our results
   
$returnMe = array();

// for every string in our batch ...
   
foreach( $batch as $str )
    {
// test it, and dump our findings into $found
       
preg_match($expr, $str, $found);

// append our findings to the placeholder
       
$returnMe[$str] = $found;
    }

    return
$returnMe;
}
?>
seth36 at gmail dot com
10-Aug-2008 11:12
For validation of email addresses, Cal Henderson's RFC 822 and RFC 2822 is_valid_email() functions rule all:

http://code.iamcal.com/php/rfc822/
Dino Korah AT webroot DOT com
09-Jul-2008 01:11
preg_match and preg_replace_callback doesnt match up in the structure of the array that they fill-up for a match.
preg_match, as the example shows, supports named patterns, whereas preg_replace_callback doesnt seem to support it at all. It seem to ignore any named pattern matched.
Tim
08-Jul-2008 05:01
I made a mistake in my previous post. Mail addresses may of course only be "exotic" in their local parts, not in the domain part. Therefore, an exotic mail address would be "exotic#%$mail@domain.com".
Tim
07-Jul-2008 11:51
For those not so familiar with regex's, I post my algorithmic email validation routine. It can more easily be changed for individual needs than regex's. My function does NOT recognize exotic email addresses as allowed by RFC. (For example, info@exotic%&$#mail.com is a legal email address but not allowed by my function.)
-Tim

<?php
function email_is_valid($email) {
   if (
substr_count($email, '@') != 1)
      return
false;
   if (
$email{0} == '@')
      return
false;
   if (
substr_count($email, '.') < 1)
      return
false;
   if (
strpos($email, '..') !== false)
      return
false;
  
$length = strlen($email);
   for (
$i = 0; $i < $length; $i++) {
     
$c = $email{$i};
      if (
$c >= 'A' && $c <= 'Z')
         continue;
      if (
$c >= 'a' && $c <= 'z')
         continue;
      if (
$c >= '0' && $c <= '9')
         continue;
      if (
$c == '@' || $c == '.' || $c == '_' || $c == '-')
         continue;
      return
false;
   }
  
$TLD = array (
        
'COM',   'NET',
        
'ORG',   'MIL',
        
'EDU',   'GOV',
        
'BIZ',   'NAME',
        
'MOBI''INFO',
        
'AERO''JOBS',
        
'MUSEUM'
     
);
  
$tld = strtoupper(substr($email, strrpos($email, '.') + 1));
   if (
strlen($tld) != 2 && !in_array($tld, $TLD))
      return
false;
   return
true;
}
?>
mailinglist dot php at hydras-world dot com
03-Jul-2008 11:30
The regexp below thinks that the e-mail address:

'me@de.com' is invalid, which it is not.

'/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@
([a-z0-9])([-a-z0-9_])+([a-z0-9])*
(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i'

I modified it and it seems to work for me in my limited tests of it.

YMMV.
brferreira at grad dot ufsc dot br
26-Jun-2008 04:48
Paperweight, this pattern worked fine for me (even for intranet adresses, like "john@localhost"; and also for subdomain emails, like "john@foo.bar.com"):
'/([a-z0-9])([-a-z0-9._])+([a-z0-9])\@
([a-z0-9])([-a-z0-9_])+([a-z0-9])
(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*/i'

but, still, this won't replace the "activation link", that is the better way to check if an e-mail is valid or not.
jonathan dot lydall at gmail dot removethispart dot com
26-May-2008 09:50
Because making a truly correct email validation function is harder than one may think, consider using this one which comes with PHP through the filter_var function (http://www.php.net/manual/en/function.filter-var.php):

<?php
$email
= "someone@domain .local";

if(!
filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo
"E-mail is not valid";
} else {
    echo
"E-mail is valid";
}
?>
Norbert
06-May-2008 02:00
Debian way is:
dpkg-reconfigure locales
Georg
04-Apr-2008 11:36
In addition to reiner-keller's comment about Umlaute using setlocale (LC_ALL, 'de_DE');

To enable 'de_DE' on my Debian 4 machine I first had to:
- uncomment 'de_DE' in file /etc/locale.gen and afterwards
- run locale-gen from the shell

preg_quote> <preg_match_all
Last updated: Fri, 02 Jan 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites