Home >>Advance PHP Tutorial >PHP Regular Expressions

PHP Regular Expressions

PHP Regular Expressions Tutorial

What is regular expressions?

The Regular expressions are basically the powerful pattern matching algorithm which can be performed in a single expression.

Arithmetic operators such as (+,-, ^) are used by the regular expressions in order to create complex expressions.

Regular expressions are known to accomplish tasks such as validating email addresses, IP address etc.

Why you should use Regular Expressions?

  • With Regular expressions it is amazingly easy to identify patterns in string data by calling a single function. Doing this saves us a good amount of coding time.
  • Regular expressions comes in handy while validating user input such as email address, domain names, telephone numbers, and IP addresses.
  • These expressions are used in highlighting keywords in search results
  • While creating a customized HTML template. Regular expressions can also be used in identifying the template tags and replace them with actual data.

Regular expressions in PHP

PHP has its built-in functions allowing the users to work with regular functions. Let's have a look at the most commonly used

regular expressions functions in the PHP

language.

 

  • preg_match –This function of regular expressions is used to execute a pattern match on a string. It simply returns true when a match is found and false when a match is not found.
  • preg_split – This function of regular expressions is used to perform a pattern match on a string followed by the splitting of results in to numeric array.
  • preg_replace -This function of regular expressions is used to perform a pattern match on a string followed by replacing the match with the specified text.

PHP Preg_match()

PHP Preg_match to test if the input conforms to the pattern defined for it. The preg_match() function requires two compulsory arguments:

  • Pattern and
  • Value(To test against the parrern).

It returns true if a match is found, and false otherwise.

Example 1(Create a Web Form that requries the username)

These are the following Constraints

  • Username :The username must be between three and eight characters long,username must contains only alphabetic Characters.

Example

<?php 
extract($_REQUEST);
if(isset($save))
{
	if(preg_match("/^[a-zA-Z ]*$/",$username))
	{
	echo "Congrates name is ok ";		
	}
	else
	{
	echo "<h3 style='color:Red'>Username not valid</h3>";
	}
}
?>
<html>
<head>
	<title>Regular Express example 1</title>
</head>
<body>
	<form method="post">
		<table>
		<tr>
		<td>Enter Your Name</td>
		<td><input type="text" name="username"/></td>
		</tr>
		<tr>
		<td colspan="2" align="center">
		<input type="submit" name="save" value="check"/></td>
		</tr>
		</table>
	</form>
</body>
</html>
Output :

Example 2:(Validating E-email Address)

A common task when working with form is involve checking email adress validation.
Here is the exampe

<?php 
extract($_REQUEST);
if(isset($save))
{
	if (filter_var($emailid, FILTER_VALIDATE_EMAIL)) 
	{
		echo "Congrates email is valid";	
	}
	else
	{
	echo "<h3 style='color:Red'>Invalid email address</h3>";
	}
}
?>
<html>
<head>
	<title>Regular Express Email validation </title>
</head>
<body>
	<form method="post">
		<table>
		<tr>
		<td>Enter Your email</td>
		<td><input type="text" name="emailid"/></td>
		</tr>
		<tr>
		<td colspan="2" align="center">
		<input type="submit" name="save" value="check"/></td>
		</tr>
		</table>
	</form>
</body>
</html>
Output :

Example 3:(Validating URL)

Here is the exampe

<?php 
extract($_REQUEST);
if(isset($save))
{
	if(preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website_url)) 
	{
	 echo "Congrates ! Web Address is Valid";
	}
	else
	{
	echo "<h3 style='color:Red'>Invalid Web address</h3>";	
	}
}
?>
<html>
<head>
	<title>Regular Express Check URL</title>
</head>
<body>
	<form method="post">
		<table>
		<tr>
		<td>Enter Your Websit URL</td>
		<td><input type="text" name="website_url"/></td>
		</tr>
		<tr>
		<td colspan="2" align="center">
		<input type="submit" name="save" value="check"/></td>
		</tr>
		</table>
	</form>
</body>
</html>
Output :

Metacharacters

Metacharacters help us to perform more complex matches pattern in the program

Metacharacters What it Means  
^ Beginning of String
$ End of String
. Any Character excepa newline character
\s A single whitespace character
\S A single non-whitespace character
\d A digit between 0 and 9
\w An alphabetic or numeric character, or underscore
[A-Z] An uppercase alphabetic character
[a-z] A lowercase alphabetic character
[0-9] A digit between 0 and 9
| Or Logical Oprator
(?= Positive conditional test
(?! Negative conditional test

No Sidebar ads