codescracker


php

PHP Regular Expression RegEx



« Previous Tutorial Next Tutorial »


With regular expression or regex in PHP, you can easily search, find, or replace any string from another. You can also perform a lot of actions using regular expression in PHP such as

Where Regular Expression Used Most

Normally regular expression in PHP used mostly in matching the correct format of email address including email id followed by at (@) sign with email company website such as gmail.com, hotmail.com etc.

PHP preg_match

The preg_match function of PHP is used for matching purposes. You will see examples of preg_match given below in this tutorial.

PHP preg_replace

The preg_replace function of PHP is used for replacing purposes. You will see the example of this function given below in this tutorial.

PHP Regular Expression Example

Below are the list of some examples on regular expression in PHP.

PHP Find Everything except Letters and Numbers

Here is an example shows how to use preg_match function of PHP to find everything except letters and numbers.

<html>
<head>
	<title>PHP Regular Expression - codescracker.com</title>
</head>
<body>
<?php
	$randomtext = "lmn@123.com";
	// below code match for everyting except letters and numbers
	$checkMatch = preg_match('/[^a-zA-Z0-9.]/', $randomtext, $matchedExceptAlphanumeric);
	if($checkMatch)
	{
		echo $matchedExceptAlphanumeric[0];
	}
	else 
	{
		echo "no match found!";
	}
?>
</body>
</html>

Below is the sample output produced by the above example code of regular expression in PHP:

php regular expression

PHP Search for Content using Regular Expression

Let's take another example of regular expression in PHP shows how to search the whole matched result and the result content between the two string.

<html>
<head>
	<title>regular expression example in php</title>
</head>
<body>
<?php
	$rtext = "regularexpressointutorialoncodescracker";
	$checkMatch = preg_match('/exp(.*?)tutorial/', $rtext, $matched);
	// the code given below prints the whole matched result
	// that is, from exp to tutorial, which is, 
	// expressiontutorial
	echo $matched[0];
	echo "<hr/>";
	// and the following code will only print the inner matched or search
	// result which is inside the bracket means between the 
	// exp and tutorial, that is, ression, here
	echo $matched[1];
?>
</body>
</html>

Below is the sample output produced by the above example code of regular expression in PHP:

php regex

PHP Extract Words from String

To extract words that is coming between two repeatable strings using regular expression function named preg_match_all available in PHP, then follow the given example code here:

<html>
<head>
	<title>regular expression example in php</title>
</head>
<body>
<?php
	$regexExampleText = "Hi computer programmer, this is regular expression tutorial.
			Hi PHP developer, this is regular expression example.
			Hi coder, this is regex tutorial.
			Hi PHP lover, this is regex example.";
	$checkRegexMatch = preg_match_all('/Hi (.*?), this/', $regexExampleText, $matched);
	if($checkRegexMatch)
	{
		foreach($matched[1] as $print_matched)
		{
			echo $print_matched;
			echo "<br/>";
		}
	}
	else 
	{
		echo "no match found..exiting..";
		exit();
	}
?>
</body>
</html>

Here is the sample output produced by the above example code:

regular expression php

PHP Search among Many Options

You can also use regular expression's preg_match() function available in PHP to search among many options as shown in the example given below:

<html>
<head>
	<title>regular expression example in php</title>
</head>
<body>
<?php
	$regexExampleText = "My contact number ends with 2";
	$checkRegexMatch = preg_match('/My contact number ends with [0123456789]/', $regexExampleText, $matched);
	if($checkRegexMatch)
	{
		echo $matched[0];
	}
	else 
	{
		echo "not found!";
	}
?>
</body>
</html>

Below is the sample output you will see in your web browser after running the above example code of regex in PHP:

regular expression example php

PHP Search for Special Characters

If you want to search for special characters such as /, ', < etc, then you have to use escape sign (\) before the special character inside preg_match function as shown in the example given below:

<html>
<head>
	<title>regular expression example in php</title>
</head>
<body>
<?php
	$regexExampleText = "I am a programmer/developer/coder.";
	$checkMatch = preg_match("/programmer\/developer\/coder/", $regexExampleText, $matched);
	if($checkMatch)
	{
		echo $matched[0];
	}
	else 
	{
		echo "not found";
	}
?>
</body>
</html>

Here is the sample output of the above example code of regex in PHP:

php regular expression example

PHP Find Every Special Characters inside String

To search and find every special characters inside the string using preg_match_all in PHP, then follow the example given below:

<html>
<head>
	<title>regular expression example in php</title>
</head>
<body>
<?php
	$regextExampleText = "123regex#tutorial&examples@codescracker.com";
	$checkMatch = preg_match_all('/[^a-zA-z0-9.]/', $regextExampleText, $matchedRegexExampleText);
	if($checkMatch)
	{
		foreach($matchedRegexExampleText[0] as $print_matched)
		{
			echo $print_matched;
		}
	}
	else 
	{
		echo "no any match found!";
	}
?>
</body>
</html>

Here is the sample output of the above example code:

regex example php

PHP Replace Every Special Characters from String

To replace every special characters from string except alphanumeric value using PHP regular expression, then follow the example given below that uses preg_replace method.

<html>
<head>
	<title>replace all special characters from string in php</title>
</head>
<body>
<?php
	$regextExampleText = "123regex#tutorial&examples@codescracker.com";
	$regexReplacedOut = preg_replace('/[^a-zA-Z0-9.]/','', $regextExampleText);
	echo $regexReplacedOut;
?>
</body>
</html>

The output produced by the above example code that shows how to replace all the special characters from string using PHP is given below:

regex php

Now you have some understanding about important of brackets such as () or [] in searching for pattern of characters using PHP regular expression functions such as preg_match or preg_replace etc.

Here are the list of most important and widely used expression.


« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test