PHP preg_match()

PHP preg_match() is a perl style regular expression function. It finds the specific pattern matches inside a string using regular expression, returns 1 if find, 0 if not, FALSE if error. The regular expression pattern must be put inside the / / delimiters. preg_match() function is much slower than other string functions such as str_replace() which do not support regular expressions.
   int preg_match(str pattern, str string [, array &matches, int flags = 0, int offset = 0])

<?PHP
	$str="2000-08-12T03:54:54Z";
	if (preg_match("/\d/",$str))
	{
	   echo "String contains number.";
	}
?>

The matches can be stored into an array which is the 3rd parameter of the function.

<?PHP
	$str="2000-08-12T03:54:54Z";
	if (preg_match("/^((\d+)\-\d+\-\d+)T/",$str,$mats))
	{
	   echo "$mats[0]";   //2000-08-12T
	   echo "$mats[1]";   //2000-08-12
	   echo "$mats[2]";   //2000
	}
?>

preg_match() function can match different patterns using | delimiter.

<?PHP
	$str="2000-08-12T03:54:54Z";
	if (preg_match("/00|03/",$str))
	{
	   echo "String contains \"03\" or \"00\"";
	}
?>

Let's see another example, and print out the order of matches:

<?PHP
$str = "The date is 2014-04-14."; 
if (preg_match("/\w+\s(\d{2}\-\d{2}-\d{4})|\w+\s(\d{4}\-\d{2}-\d{2})/", $str, $mats))
{
	for ($ii=0;$ii<count($mats);$ii++)
	{
		echo "$ii: $mats[$ii]\n";
	}
}
?>

The output is:
0: is 2014-04-14
1:
2: 2014-04-14

preg_match() can be case insensitive by using i modifier after the delimiter /.

<?PHP
	$str = "match Can Be Case Insensitive with i modifier";
    if (preg_match("/can/i",$str)) echo "True"; else "False"; //True
?>

preg_match_all() can get the number of all matches. preg_match() will only return 1 if match is found.

<?PHP
	$str = "match Can Be Case Insensitive with i modifier";
    echo preg_match_all("/se/",$str); //2
    echo preg_match("/se/",$str);	//1
?>

Let's list all the matches:

<?PHP
$str = "This is the website <a href=\"http://www.endmemo.com\">endmemo</a> and google website <a href=\"http://www.google.com\">google</a>."; 
if (preg_match_all("/\<a href=\"([^\>]+)\"\>([^\>]+)\<\/a\>/i", 
	$str, $mats, PREG_SET_ORDER) > 0)
{
	 for ($ii=0;$ii<count($mats);$ii++)
	 {
		 $groups = $mats[$ii];
		 $core = $groups[0];
		 $site = $groups[1];
		 $name = $groups[2];
		 echo "$ii: $core, $site, $name\n";
	}
}
?>

The output is:
0: <a href="http://www.endmemo.com">endmemo</a>, http://www.endmemo.com, endmemo
1: <a href="http://www.google.com">google</a>, http://www.google.com, google

If PREG_SET_ORDER parameter is not specified, the returned matches are in different order:

<?PHP
$str = "This is the website endmemo and google website google."; 
if (preg_match_all("/\^lt;a href=\"([^\>]+)\"\>([^\>]+)\<\/a\>/i", 
	$str, $mats) > 0)
{
	 for ($ii=0;$ii<count($mats);$ii++)
	 {
		 $groups = $mats[$ii];
		 echo "$ii: ";
		 for ($jj=0;$jj<count($groups);$jj++)
		 {
			 echo "$groups[$jj], ";
		 }
		 echo "\n";
	}
}
?>

The output is:
0: <a href="http://www.endmemo.com">endmemo</a>, <a href="http://www.google.com">google</a>,
1: http://www.endmemo.com, http://www.google.com,
2: endmemo, google,


Similiar functions include ereg_replace(), ereg_match(), preg_replace(), str_replace().


:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • regular expr. • str_replace
 • strcmp • strpos
 • strrev • strrpos
 • strtr • substr
 • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home