php
PHP - Regular expressions
PHP Regular expressions are a very useful to web developers. 
PHP has built in functions that allow us to work with regular functions. Let’s now look at the commonly used regular expression functions in PHP.  

PHP has implemented quite a few regex functions which uses different parsing engines. There are two major parser in PHP. 
  • POSIX Regular Expressions
  • PERL Regular Expressions

POSIX Regular Expressions

To use posix regular expressions first you need to learn the syntax. This syntax consists in a series of letters, numbers, dots, hyphens and special signs, which we can group together using different parentheses etc,.
Operator Description
^ The circumflex symbol marks the beginning of a pattern, although in some cases it can be omitted
$ Same as with the circumflex symbol, the dollar sign marks the end of a search pattern
. The period matches any single character
? It will match the preceding pattern zero or one times
+ It will match the preceding pattern one or more times
* It will match the preceding pattern zero or more times
| Boolean OR
Matches a range of elements
() Groups a different pattern elements together
[] Matches any single character between the square brackets
{min, max} It is used to match exact character counts
\d Matches any single digit
\D Matches any single non digit caharcter
\w Matches any alpha numeric character including underscore (_)
\W Matches any non alpha numeric character excluding the underscore character
\s Matches whitespace character
As an addition in PHP the forward slash character is escaped using the simple slash \. Example: " /le\/arn/ "
Example Description
‘/learn/’ It will match the word learn
‘/^learn/’ It will match learn at the start of a string. Possible matches are learn or learnworld, but not worldlearn
‘/learn$/’ It will match learn at the end of a string.
‘/le.n/’ It will match any character between le and n.
‘/le?arn/’ It will match either arn or learn
‘/learn+/’ It will match learn on or more time. E.g. learn or learnlearn
‘/le*arn/’ Matches arn, learn or lelearn, but not learnoo
‘/learn|world/’ It will either match the word learn or world
‘/(A-Z)/’ Using it with the hyphen character, this pattern will match every uppercase character from A to Z. E.g. A, B, C…
‘/[abc]/’ It will match any single character a, b or c
‘/abc{1}/’ Matches precisely one c character after the characters ab. E.g. matches abc, but not abcc
‘/abc{1,}/’ Matches one or more c character after the characters ab. E.g. matches abc or abcc
‘/abc{2,4}/’ Matches between two and four c character after the characters ab. E.g. matches abcc, abccc or abcccc, but not abc

PERL Regular Expressions

PHP offers the following functions for searching strings using Perl-compatible regular expressions :
  • preg_match()
  • preg_match_all()
  • preg_replace()
  • preg_split()
  • preg_grep()
  • preg_quote()
preg_match()

The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.

The preg_match() function to perform a simple pattern match for the word guru in a given URL.

<!DOCTYPE html>
<html>
<head>
    <title>PHP Regular Expression - Preg_match()</title>
</head>
<body>

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

</body>
</html>
Output :
preg_match_all()

The preg_match_all() function matches all occurrences of pattern in string.

<!DOCTYPE html>
<html>
<head>
    <title>PHP Regular Expression - Preg_match_all()</title>
</head>
<body>

<?php
   $web_info = "Name : <b>Free Time Learning</b> <br /> URL : <b>www.freetimelearning.com</b>";
   preg_match_all ("/<b>(.*)<\/b>/U", $web_info, $prg_math_array);
   
   print $prg_math_array[0][0]." <br> ".$prg_math_array[0][1]."\n";
?>

</body>
</html>
Output :
preg_replace()
The preg_replace() function that performs a pattern match and then replaces the pattern with something else.

The code below searches for the word guru in a string.

It replaces the word guru with the word guru surrounded by css code that highlights the background color.

<!DOCTYPE html>
<html>
<head>
    <title>PHP Regular Expression - preg_replace()</title>
</head>
<body>

<?php
$text = "The freetimelearning is the best educational web application : www.freetimelearning.com";
$text = preg_replace("/freetimelearning/", '<span style="background:#0099da; color:#FFF;">freetimelearning</span>', $text);
echo $text;

?>

<h4>(or)</h4>

<?php
   $copy_rights = "all &copy; rights reserved by 2016";
   $copy_rights = preg_replace("([0-9]+)", '<span style="background:#0099da; color:#FFF;">2017</span>', $copy_rights);
   
   print $copy_rights;
?>

</body>
</html>
Output :
preg_split()

The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.

flags can be any combination of the following flags :

PREG_SPLIT_NO_EMPTY : If this flag is set, only non-empty pieces will be returned by preg_split().

PREG_SPLIT_DELIM_CAPTURE : If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.

PREG_SPLIT_OFFSET_CAPTURE : If this flag is set, for every occurring match the appendant string offset will also be returned.

<!DOCTYPE html>
<html>
<head>
    <title>PHP Regular Expression - preg_split()</title>
</head>
<body>

<?php
	$my_text="Free Time Learning";
	
	$my_array  = preg_split("/ /", $my_text);
	
	print_r($my_array );
?>

</body>
</html>
Output :
preg_grep()
If flag is set to PREG_GREP_INVERT, this function returns the elements of the input array that do not match the given pattern.
<!DOCTYPE html>
<html>
<head>
    <title>PHP Regular Expression - preg_grep()</title>
</head>
<body>

<?php
   $books = array("javascript", "php", "bootstrap", "MySQL");
   
   // find elements beginning with "p", followed by one or more letters.
   $p_books = preg_grep("/p(\w+)/", $books);
   
   print "The Book is : " . $p_books[0] ."<br />";
   print "The Book is : " . $p_books[1] ."<br />";
   print "The Book is : " . $p_books[2];
?>


</body>
</html>
Output :
preg_quote()
The php preg_quote() function takes str and puts a backslash in front of every character that is part of the regular expression syntax.
<!DOCTYPE html>
<html>
<head>
    <title>PHP Regular Expression - preg_quote()</title>
</head>
<body>

<?php
   $p_q = '$18 for a ftl 3/180';
   $p_q = preg_quote($p_q, '/');
   
   echo $p_q;
?>

</body>
</html>
Output :