Wednesday, July 18, 2012

Complex regular expressions (regex) in php

Regular expressions are a very good way to check if a text meets several standards or to search for patterns.

There are several regex that are most used by programmers.

I found out that many programmers don't really understand how regex works. Sometimes, the best way to learn regex is by example.

Validating an email address:

  \b[a-z|0-9|\.|_|%|\+|\-]+@[a-a|0-9|\.|-]+\.[a-z]{2,4}\b

This regular expressions will validate email addresses on a word boundary, and is best to use it when you search for email addresses in a text. If you want, for example, to validate an email address from a text input, you should use start and end strings (^ is the start string and $ is the end string).

  ^[a-z|0-9|\.|_|%|\+|\-]+@[a-a|0-9|\.|-]+\.[a-z]{2,4}$

Searching for images in HTML code

 
  /<img[ ]*([\w]+)=["|\']([^("|\')]+)["|\'](?:[ ]*([\w]+)=["|\']([^("|\')]+)["|\'])?(?:[ ]*([\w]+)=["|\']([^("|\')]+)["|\'])?(?:[ ]*([\w]+)=["|\']([^("|\')]+)["|\'])?(?:[ ]*([\w]+)=["|\']([^("|\')]+)["|\'])?[^>]*>/i

IMPORTANT! You must add this regex on a single line.

Matched on a html code with images, this will populate an array with src,alt and title attributes of the images. For example: if we have this text:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer turpis arcu, ultricies ut luctus viverra, egestas in eros. <img id="img1" src="img/image 1.jpg" alt="Image 1" />Aliquam vel mi leo. Sed convallis ipsum at diam ornare vel suscipit turpis adipiscing. Curabitur sagittis,<img title="Title 2" src="img/image-2.jpg" id="img2" alt="Image 2" /> eros vel laoreet scelerisque, felis nunc hendrerit dui, id egestas nisl lectus id ipsum.</p>

The regex will return:

array
  0 => string '<img id="img1" src="img/image 1.jpg" alt="Image 1" />' (length=53)
  1 => string 'id' (length=2)
  2 => string 'img1' (length=4)
  3 => string 'src' (length=3)
  4 => string 'img/image 1.jpg' (length=15)
  5 => string 'alt' (length=3)
  6 => string 'Image 1' (length=7)

No comments:

Post a Comment