What is the use of preg_match in php?
❮ PHP RegExp Reference Show ExampleUse a regular expression to do a case-insensitive search for "w3schools" in a string: $str = "Visit W3Schools"; Try it Yourself » Definition and UsageThe Syntaxpreg_match(pattern, input, matches, flags, offset) Parameter Values
Technical Details
More ExamplesExampleUse PREG_OFFSET_CAPTURE to find the position in the input string in which the matches were found: $str = "Welcome to W3Schools"; Try it Yourself » ❮ PHP RegExp Reference (PHP 4, PHP 5, PHP 7, PHP 8) preg_match — Perform a regular expression match Descriptionpreg_match( Parameterspattern The pattern to search for, as a string. subject The input string. matches If flags PREG_OFFSET_CAPTURE If this flag is passed, for every occurring match the appendant
string offset (in bytes) will also be returned. Note that this changes the value of
The above example will output: Array ( [0] => Array ( [0] => foobarbaz [1] => 0 ) [1] => Array ( [0] => foo [1] => 0 ) [2] => Array ( [0] => bar [1] => 3 ) [3] => Array ( [0] => baz [1] => 6 ) ) PREG_UNMATCHED_AS_NULL If this flag is passed, unmatched subpatterns are reported as
The above example will output: array(4) { [0]=> string(2) "ac" [1]=> string(1) "a" [2]=> string(0) "" [3]=> string(1) "c" } array(4) { [0]=> string(2) "ac" [1]=> string(1) "a" [2]=> NULL [3]=> string(1) "c" } offset Normally, the search starts from the beginning of the subject string. The optional parameter
Return Values preg_match() returns 1 if the Warning This function may return Boolean Errors/Exceptions If the regex pattern
passed does not compile to a valid regex, an Changelog
ExamplesExample #1 Find the string of text "php"
Example #2 Find the word "web"
preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) { Example #3 Getting the domain name out of a URL
The above example will output: Example #4 Using named subpattern
= 'foobar: 2008';preg_match('/(?P The above example will output: Array ( [0] => foobar: 2008 [name] => foobar [1] => foobar [digit] => 2008 [2] => 2008 ) NotesTip Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() instead as it will be faster. See Also
force at md-t dot org ¶ 11 years ago
MrBull ¶ 11 years ago
ruakuu at NOSPAM dot com ¶ 12 years ago
preg_match($pattern, $match_string)) { cebelab at gmail dot com ¶ 12 years ago
yofilter-php at yahoo dot co dot uk ¶ 9 years ago
workhorse at op dot pl ¶ 11 years ago
ulli dot luftpumpe at murkymind dot de ¶ 10 years ago
$subject = file_get_contents('myfile.txt'); arash dot hemmat at gmail dot com ¶ 11 years ago
daevid at daevid dot com ¶ 13 years ago
mohammad40g at gmail dot com ¶ 11 years ago
andre at koethur dot de ¶ 9 years ago
solixmexico at outlook dot com ¶ 5 years ago
sainnr at gmail dot com ¶ 11 years ago
ian_channing at hotmail dot com ¶ 11 years ago
cmallabon at homesfactory dot com ¶ 11 years ago
geompse at gmail dot com ¶ 5 years ago
corey [works at] effim [delete] .com ¶ 13 years ago
= preg_replace($regex, '($1) $2-$3 ext. $4', $phoneNumber);
// or, provided you use the $matches argument in preg_match
$formatted = "($matches[1]) $matches[2]-$matches[3]";
Kae Cyphet ¶ 12 years ago
splattermania at freenet dot de ¶ 12 years ago
Anonymous ¶ 10 years ago
danielrydell at gmail dot com ¶ 5 years ago
aer0s ¶ 10 years ago
Jonny 5 ¶ 10 years ago
akniep at rayo dot info ¶ 13 years ago
= "test="; Nimja ¶ 10 years ago
= array('word1', 'word2', 'word3', 'word4', 'word5', 'word6', 'word7', 'word8', 'word9', 'word10', 'word11', 'word12' ); SoN9ne at gmail dot com ¶ 12 years ago
replaceAnchorsWithText($input).' Yousef Ismaeil Cliprz ¶ 9 years ago
preg_match('/(base64_|eval|system|shell_|exec|php_)/i',$contents)) ASchmidt at Anamera dot net ¶ 2 years ago
ayman2243 at gmail dot com ¶ 11 years ago
$split_subject as $k => $v){
ian_channing at hotmail dot com ¶ 13 years ago
preg_match($regex, $vat_number);
jphansen at uga dot edu ¶ 10 years ago
Frank ¶ 11 years ago
skds1433 at hotmail dot com ¶ 13 years ago
'; Stefan ¶ 12 years ago
$test as $key=>$value) {
itworkarounds at gmail dot com ¶ 11 years ago
Anonymous ¶ 12 years ago
matt at proweb dot co dot uk ¶ 1 year ago
marcosc at tekar dot net ¶ 13 years ago
chris at ocproducts dot com ¶ 1 year ago
ASchmidt at Anamera dot net ¶ 2 years ago
plasma ¶ 12 years ago
= 'http://name:pass@';
wjaspers4 [at] gmail [dot] com ¶ 13 years ago
chat dot noir at arcor dot de ¶ 5 years ago
matt ¶ 13 years ago
xcsv at gmx dot net ¶ 2 years ago
phil dot taylor at gmail dot com ¶ 13 years ago
= "";
What is used of Preg_match?PHP | preg_match() Function. This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.
What does Preg_match mean in PHP?preg_match() in PHP – this function is used to perform pattern matching in PHP on a string. It returns true if a match is found and false if a match is not found. preg_split() in PHP – this function is used to perform a pattern match on a string and then split the results into a numeric array.
What does Preg_match_all return?The preg_match_all() function returns the number of matches of a pattern that were found in a string and populates a variable with the matches that were found.
What value is return by Preg_match function if pattern exist?preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .
|