This PHP function is usually use in searching a string. It compares the two strings (search_pattern and string_given) and return 0 or 1 value. 1 denotes successful return while 0 is failure.
search_pattern – string which formatted in regular expression.
$string_given - a given string that you’re comparing to.
Here are some basic regular expression in using preg_macth function.
Important Notes Before you begin.
1. Preg_macth is case sensitive so better to user strtolower function in your $string given parameter.
2. Observe "/ /" format. Regular expressions are formed by starting with a forward slash /, followed by a sequence of special symbols and words to match, then another slash and, optionally, a string of letters that affect the expression.
BASIC
to see more examples visit php.net
Preg_Match Parameter
preg_match(search_patter, $string_given)
search_pattern – string which formatted in regular expression.
$string_given - a given string that you’re comparing to.
When to use Preg_match function?
Preg_match is usually used for search string functionality, string validation, manipulation of text. The most common use of preg_match is in validating email, phone number, name etc….Here are some basic regular expression in using preg_macth function.
Important Notes Before you begin.
1. Preg_macth is case sensitive so better to user strtolower function in your $string given parameter.
2. Observe "/ /" format. Regular expressions are formed by starting with a forward slash /, followed by a sequence of special symbols and words to match, then another slash and, optionally, a string of letters that affect the expression.
BASIC
<?php if (preg_match("/test/", "test")) { echo "Got match!\n"; } if (preg_match("/php/i", "php")) { echo "Got match!\n"; } ?>EMAIL
<?php
if (preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email)){
echo "Email is valid!\n";
}
?>
DOMAIN<?php preg_match('@^(?:http://)?([^/]+)@i', "http://www.webdosh.net/index.html", $match); $hostname = $match[1]; // get last two segments of host name preg_match('/[^.]+\.[^.]+$/', $hostname, $match); echo "domain name is: {$match[0]}\n"; ?>PHONE
<?php if(! preg_match('/^\d{2}(-\d{3}){2}(\d{2})?$/', $phone)) { echo "<p> Error message</p>"; } ?>
to see more examples visit php.net
1 comments:
I appreciate, because I found exactly what I was looking for. God Bless you man. Have a great day.
Post a Comment