Developer Snippet Diary

Regular expressions in php

Functions:

syntax: function_name('/pattern/',string);

preg_match –find pattern match on a string. returns true if found else false

preg_split – find pattern match on a string and then split the results into a numeric array

preg_replace – find pattern match on a string and then replace the match with the specified text.

USE OF REGULAR EXPRESSIONS

  • identifying patterns in the string,
  • validating user input,
  • Highlighting keywords in search results, 

preg_match

<?php
$my_url = "www.rizikmw.com";
if (preg_match("/rizikmw/", $my_url))
{
	echo "the url $my_url contains rizikmw";
}
else
{
	echo "the url $my_url does not contain rizikmw";
}
?>

preg_split

<?php
$my_text="I Love R";
$my_array  = preg_split("/ /", $my_text);
print_r($my_array );
?>

preg_replace

<?php
$text = "We are Gondals and Gondals are amazing.";
$text = preg_replace("/Gondals/", '<span style="background:yellow">Gondals</span>', $text);
echo $text;
?>

SOME REGULAR EXPRESSIONS

  • echo "<b>Hello 0ops 03468434644</b><br/>";
  • $str="Hello 0ops 03468434644";
  • echo preg_replace("/[4o]/", "X", $str)."<br/>";//replace all 4 with X
  • echo preg_replace("/./", "X", $str)."<br/>";//replace every chraters by X, XXXXXXXXXXX
  • echo preg_replace("/...../", "X", $str)."<br/>";//replace every five chraters (left to right) by X and print remaining, XXXX44
  • echo preg_replace("/^Hello/", "X", $str)."<br/>"; // any string that starts with hello
  • echo preg_replace("/44$/", "X", $str)."<br/>"; /// 44$/ matches all strings that have 44 at end. check .com domains using this .com$
  • echo preg_replace("/0346+8434644/", "X", $str)."<br/>"; //preceding value must be Gon
  • echo preg_replace("/Gon\+d/", "X", $str)."<br/>"; //treat + as string, use \escape ,this is not for preceding
  • echo preg_replace("/[a]/", "X", $str)."<br/>"; //replace all a with X
  • echo preg_replace("/[a-z]/", "X", $str)."<br/>"; //replace all lower case charters with X
  • echo preg_replace("/[A-Z]/", "X", $str)."<br/>";//replace all upper case charters with X
  • echo preg_replace("/[0-9]/", "X", $str)."<br/>";//replace all 0,1,2,3,4,5,6,7,8,9 with X
  • echo preg_replace("/[0-3]/", "X", $str)."<br/>";//replace all 0,1,2,3 with X
  • echo preg_replace("/[4]/", "X", $str)."<br/>";//replace all 4 with X
  • echo preg_replace("/[4p]/", "X", $str)."<br/>";//replace all 4 or p with X
  • $my_email = "00@company.com";
    if (preg_match("/^[a-zA-Z0-9._-]{2,3}+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $my_email)) {
    //"^[a-zA-Z0-9._-]" matches any lower or upper case letters, numbers between 0 and 9 and dots, underscores or dashes.
    //"+@[a-zA-Z0-9-]" matches the @ symbol followed by lower or upper case letters, numbers between 0 and 9 or dashes.
    //"+\.[a-zA-Z.]{2,5}$/" escapes the dot using the backslash then matches any lower or upper case letters with a character length between 2 and 5 at the end of the string.
    echo "$my_email is a valid email address";
    }
    else
    {
    echo "$my_email is NOT a valid email address";
    }
    
    

Extract specific data from string it will extract fafa no 1

$str=mynama is done and i am fafa no 1: ok
preg_match('/fa-(.*?):/', $str, $res);
pritn_r($res);

Split stric on basis on one or more spaces like tab

$parts = preg_split('/\s+/', $value);

Regular Expressions Cheat Sheet by DaveChild - Download free from Cheatography - Cheatography.com: Cheat Sheets For Every Occasion

Posted by: R GONDAL
Email: rizikmw@gmail.com