(PHP 4, PHP 5, PHP 7, PHP 8) Show Table of Contents
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 ¶ 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
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
solixmexico at outlook dot com ¶ 5 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
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
workhorse at op dot pl ¶ 11 years ago
aer0s ¶ 10 years ago
Kae Cyphet ¶ 12 years ago
splattermania at freenet dot de ¶ 13 years ago
Anonymous ¶ 10 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]";
danielrydell at gmail dot com ¶ 5 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)) 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
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
skds1433 at hotmail dot com ¶ 13 years ago
'<form method="post">'; marcosc at tekar dot net ¶ 13 years ago
chris at ocproducts dot com ¶ 1 year ago
ASchmidt at Anamera dot net ¶ 2 years ago
ASchmidt at Anamera dot net ¶ 2 years ago
plasma ¶ 12 years ago
= 'http://name:[email protected]';
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
= "";
If the string to parse is also the page url then you could use $_SERVER["REQUEST_SCHEME"] which returns 'http' or 'https' or $_SERVER["HTTPS"] which returns 1 if the scheme is https. Method 1: strpos() Function: The strpos() function is used to find the first occurrence of a sub string in a string. If sub string exists then the function returns the starting index of the sub string else returns False if the sub string is not found in the string (URL). HTMLInputElement. checkValidity() method is used to check if a string in <input> element's value attribute is URL . The checkvalidity() method returns true if the value is a proper URL and false if the input is not a proper URL. function validateURL(link) { if (link. indexOf("http://") == 0 || link. indexOf("https://") == 0) { console. log("The link has http or https."); } else{ console. How to check if URL is http or https in JavaScript?function validateURL(link) { if (link. indexOf("http://") == 0 || link. indexOf("https://") == 0) { console. log("The link has http or https."); } else{ console.
How do I check if a string contains https?To check if string starts with “http”, use PHP built-in function strpos(). strpos() takes the string and substring as arguments and returns 0, if the string starts with “http”, else not.
How do you check if a string is a URL in Javascript?checkValidity() method is used to check if a string in <input> element's value attribute is URL . The checkvalidity() method returns true if the value is a proper URL and false if the input is not a proper URL.
|