Current Location: Home> Function Categories> preg_match

preg_match

Perform regular expression matching
Name:preg_match
Category:Regular processing PCRE
Programming Language:php
One-line Description:Find the first match of the pattern in the string.

Definition and usage

preg_match() function returns whether a match is found in the string.

Example

Example 1

Use regular expressions to search for "w3school" in a string with case-insensitive:

 <?php
$str = "Visit W3School" ;
$pattern = "/w3school/i" ;
echo preg_match ( $pattern , $str ) ;
?>

Try it yourself

Example 2

Use PREG_OFFSET_CAPTURE to find the location of the match in the input string:

 <?php
$str = "Welcome to W3School" ;
$pattern = "/w3school/i" ;
preg_match ( $pattern , $str , $matches , PREG_OFFSET_CAPTURE ) ;
print_r ( $matches ) ;
?>

Try it yourself

Similar Functions
Popular Articles