Skip to content

PHP Regular Expressions

PHP regular expressions seems quite complicated. Historically regular expression were originally designed to help working with strings under unix systems. A regular expression is a pattern, that can find a pattern in a string and/or replace it if you want. It can do very simple and complex string manupulations helping your application validate, compare, compute, decide etc. You can match phone numbers, email addresses, url’s, credit card numbers, social security numbers, zip codes, states, cities…..list never ends.

Regular Expression types

There are two types of regular expressions they are,

  • POSIX Extended.
  • Perl Compatible.

The ereg, eregi_replace,ereg_replace,… are the POSIX versions and preg_match, preg_replace,…. are the perl versions. It is important that, while using perl compatible regular expressions, the expressions should be enclosed in the delimiters, a forward slash (/).

The regular expressions basic syntax

To use regular expressions first you need to learn the syntax of the patterns. We can group the characters inside a pattern like this:

  • Normal characters which match themselves like hello
  • Start and end indicators as ^ and $
  • Count indicators like +,*,?
  • Logical operator like |
  • Grouping with {},(),[]

Examples

<?php

$text = "PHP is a server side scripting language";
/* 'i' after the delimiters indicates case-insensitive search */
if(preg_match("/php/i",$text));
    echo "Word found";
else
    echo "word not found";
?>

<?php /* \b after delimiters indicates word boundary, the distinct word 'script' is matches not the partial words like scripting etc.. */ preg_match("/\bscript\b/",$text) ?>
<?php /* validating a email,the pattern will be, ^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)* (\.[a-z]{2,4})$ */ $pattern = "/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-] +)*(\.[a-z]{2,4})$/"; /* 1. ^ and $ are start and end indicators. 2. [_a-z0-9-] - the username can be in small(a-z), numeric(0-9), special characters like (_-).it allows usernames such as aravind, aravi123d45 etc. 3. .[_a-z0-9-] the username following '.' i.e. it allows such as aravind.ms, ms.aravin123d34 etc. 4. this is similar for patterns after '@' and '.' 5. {2,4} the alphabetical string must consist of no less than two and not more than four characters. */ $email = "name@somedomain.com"; if(preg_match($pattern, $email)) echo "valid email"; else echo "Invalid email"; ?>
<?php // get host name from the url preg_match('@^(?:http://)?([^/]+)@i', "https://aravindtrue.wordpress.com/php/", $matches); echo $host = $matches[1]; // ouputs aravindtrue.wordpress.com // get last two segments of host name preg_match('/[^.]+\.[^.]+$/', $host, $matches); echo "domain name is: {$matches[0]}"; /
/ ouputs wordpress.com ?>

Now, You are ready to dive into PHP Sea (Official PHP Website).

<< Back

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: