Tuesday, July 16

Basic functionality of Preg_match in PHP with various scenarios and its example

Hi All,

Basic regexps with preg_match()

syntax:


int preg_match ( string pattern, string subject [, array matches [, int flags [, int offset]]])


preg_match() and takes two parameters: the pattern to match, and the string to match it against.

Preg_match() will apply the regular expression in parameter one to the string in parameter two and see whether it finds a match - if it does, it will return 1, otherwise 0. 

 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.  

Note:

  •   The “i” used to find  pattern delimiter indicates a case-insensitive search.

  •  The “\b”used to find pattern indicates a word boundary, so only the distinct.

 

<?php
echo "<br>Basic preg_match Functionality <br><br> ";
echo "example 1 for Case Sensitive Search <br>";
if(preg_match("/cool/", "I love to share Cool things that help others.")){
        echo "matched text<br>";
} else {
        echo "Not Matched text<br>";
}

echo "<br> example 2 for Case Insensitive  Search<br>";
if(preg_match("/bala/i", "my name is Balamurugan shortly call has a Bala")){
        echo "name found in text<br>";
}else{
        echo "name is not found in text<br>";
}


echo "<br> example 3 for Word boundary <br>";
if(preg_match("/\bkrishna\b/i",  "Krishna is a god. he kills kamsan in long long ago. and Krishna became king" )){
    echo " krishna name <br>";
}else{
    echo " krishna name not found<br>";
}

echo "<br> example 4 for Found string matched <br>";
if(preg_match("/[Ff]oo/","foo")){
    echo 'string matched<br>';
}  else {
    echo 'String not matched<br>';
}

echo "<br> example 5 for Found string unmatched <br>";
if(preg_match("/[^Ff]oo/","foo")){
    echo 'string matched<br>';
}  else {
    echo 'String not matched<br>';
}

echo "<br> example 6 for Alpha Numeric <br>";
if(preg_match("/[0-9,A-Z,a-z]/","foo")){
    echo 'It is Alphanumeric<br>';
}  else {
    echo 'It is not Alphanumeric<br>';
}

echo "<br> example 7 for Numeric <br>";
if(preg_match("/[0-9]/","foo")){
    echo 'It is Numeric<br>';
}  else {
    echo 'It is not Numeric<br>';
}

echo "<br> example 8 for Lowercase of string <br>";
if(preg_match("/[a-z]/","fo0")){
    echo 'It is Lowercase<br>';
}  else {
    echo 'It is not Lowercase<br>';
}

echo "<br> example 9 for Uppercase of string <br>";
if(preg_match("/[A-Z]/","foo")){
    echo 'It is uppercase<br>';
}  else {
    echo 'It is not Uppercase<br>';
}

echo "<br> example 10 for matched of character in string <br>";
if(preg_match("/[a-w]orld/","aorld")){
    echo 'It is matched<br>';
}  else {
    echo 'It is not matched<br>';
}

echo "<br> example 11 for matched of character & number in string <br>";
if(preg_match("/[a-t]est[0-9][0-9]/","west33")){
    echo 'It is matched<br>';
}  else {
    echo 'It is not matched<br>';
}

echo "<br> example 12 for matched of character & number in string in_casesensitive <br>";
if(preg_match("/[a-t]est[0-9][0-9]/i","West33")){
    echo 'It is matched<br>';
}  else {
    echo 'It is not matched<br>';
}

echo "<br> example 13 for not matched of character & number in string <br>";
if(preg_match("/[^a-t]est[0-9][0-9]/","Best33")){
    echo 'It is matched<br>';
}  else {
    echo 'It is not matched<br>';
}

echo "<br> example 14 for not matched of character & number in string in_casesensitive <br>";
if(preg_match("/[^a-t]est[0-9][0-9]/i","Best33")){
    echo 'It is matched<br>';
}  else {
    echo 'It is not matched<br>';
}

?>

OUTPUT

~~~~~~~~



Basic Preg_Match Functionality

example 1 for Case Sensitive Search
Not Matched text

example 2 for Case Insensitive Search
name found in text

example 3 for Word boundary
krishna name

example 4 for Found string matched
string matched

example 5 for Found string unmatched
String not matched

example 6 for Alpha Numeric
It is Alphanumeric

example 7 for Numeric
It is not Numeric

example 8 for Lowercase of string
It is Lowercase

example 9 for Uppercase of string
It is not Uppercase

example 10 for matched of character in string
It is matched

example 11 for matched of character & number in string
It is not matched

example 12 for matched of character & number in string in_casesensitive
It is not matched

example 13 for not matched of character & number in string
It is matched

example 14 for not matched of character & number in string in_casesensitive
It is not matched
 

No comments:

Post a Comment

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...