HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
PHP RegExp Reference : Use a regular expression to do a case-insensitive search for "W3C" in astring
The preg_match()
function returns whether a
match was found in a string.
Parameter | Description |
---|---|
pattern | Required. Contains a regular expression indicating what to search for |
input | Required. The string in which the search will be performed |
matches | Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found |
flags | Optional. A set of options that change how the matches array is
structured:
|
offset | Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter |
Return Value: | Returns 1 if a match was found, 0 if no matches were found and false if an error occurred |
---|---|
PHP Version: | 4+ |
Changelog: | PHP 7.2 - Added the PREG_UNMATCHED_AS_NULL flag PHP 5.3.6 - The function returns false when the offset is longer than the length of the input PHP 5.2.2 - Named subpatterns can use the (?'name') and (? <name>) syntax in addition to the previous (?P<name>) |
Use PREG_OFFSET_CAPTURE to find the position in the input string in which the matches were found:
<?php
$str = "Welcome to w3resource";
$pattern = "/w3resource/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
❮ PHP RegExp Reference