Top10WebHosting

Open NavigationOpen Search
Everything related to web hosting.
  • Learning center
    • Move host guide
    • Compare Shared Hosting & VPS
    • SEO related
  • Hosting by features
    • Top web hosting
    • cPanel web hosting
    • PayPal web hostings
    • PHP Web Hosting
    • VPS Web Hosting
  • Coupons
    • Bluehost coupon 2017
  • Hosts overview
    • GreenGeeks Review
    • JustHost Overview.
    • Bluehost Overview
    • IXWebhosting Overview
    • Hostmonster Overview
    • Hostgator Overview.
    • SiteGround Overview
  • About
    • Contact Us
    • Disclosure
You are here: Home / php / PHP Regular Expression tool.

PHP Regular Expression tool.

December 18, 2008 by Wyng

I was trying to create a php code to parse some text and to process it.

I have difficult time with the substr (sub string), strpos (which return the position of a particular segment of string). Testing and debugging it is just a time consuming work.

Luckily my friend enlighten me by introducing me regular expression way to look for values in a text. Using regular expressions you can easy find a pattern in a string and/or replace it if you want.

BUT PHP regular expressions seems to be a quite complicated area especially when I am not a experienced Unix user.Historically regular expressions were originally designed to help working with strings under Unix systems.

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 {},(),[]

An example pattern to check valid emails looks like this:

Code:
^[a-zA-Z0-9._-][email protected][a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$ The code to check the email using Perl compatible regular expression looks like this:
  1. $pattern = “/^[a-zA-Z0-9._-][email protected][a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/”;
  2. $email = “[email protected]”;
  3. if (preg_match($pattern,$email)) echo “Match”;
  4. else echo “Not match”;
The syntax is not easy derive when you are not familiar with it. After googling around, I found http://regex.larsolavtorvik.com
This AJAX tool allows you to enter sample text string and your pattern string. The tool is able to execute it on the fly and shows result immediately. It will also print out the PHP source code. This really save my time from hard coding pattern and test string.

Filed Under: php, web development

Back to top ▴

List of Most popular hosting plan


Our list of top web hosting plan with discounted entry price, good features with excellent support.

Hosts using the Most user friendly control panel


cPanel control panel has the most user friendly interface to control the Linux enviroment.

Hosts accepting PayPal


Stay safe, use Paypal to hide your credit cards from hosts.

Follow us on Social media

About Top10WebHosting.com

I manage & consult web developments for companies. And I have chance to use different web hosts by different client.

I am here to share some good & highlights of different hosts by features & rating.

Search this website

Copyright © Top10WebHosting.com 2002-2018. All Rights Reserved.

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok