(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<name>\w+): (?P<digit>\d+)/', $str, $matches);/* Alternative */ 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 ¶ 10 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 ¶ 9 years ago
danielrydell at gmail dot com ¶ 4 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).'<hr/>'; 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
'<form method="post">'; 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
= "";
|