Really useful RegEx bits

If like me, you often need to use regular expressions, but still after over 10 years of using them can waste an entire morning just trying to write a simple search and replace, here are some really useful snippets I have used along the way.

To match an HTML link within a string, and to isolate the inner HTML, (whatever occurs between the opening and closing anchor tags), use:

<?php
preg_match('#<a\s+.*?href=[\'"]([^\'"]+)[\'"]\s*
    (?:title=[\'"]([^\'"]+)[\'"])?.*?>((?:(?!</a>).)*)</a>#i', $input, $match);

// $input - A STRING CONTAINING AN ANCHOR LINK WITH INNER HTML
$input = 'This is some text containing a linked image <a
    href="http://www.google.com"><img src="google.jpg" alt="Google" 
    width="200" height="50" /></a>. Goodbye.';

// $match[0] RETURNS THE ENTIRE LINK INCLUDING THE INNER HTML AND ANCHOR TAGS
$match[0] == '<a href="http://www.google.com"><img src="google.jpg"
    alt="Google" width="200" height="50" /></a>';

// $match[1] RETURNS JUST THE ANCHOR LINK
$match[1] == 'http://www.google.com';

// $match[2] RETURNS THE INNER HTML, IN THIS CASE, JUST THE IMAGE TAG
$match[2] == '<img src="google.jpg" alt="Google" width="200" height="50" />';
?>

To match an email address, and to isolate either the username or domain name part of the address, use:

<?php
preg_match('#([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i',
    $input, $match);

// $input - A STRING CONTAINING AN EMAIL ADDRESS
$input = 'My email address is spam-a-lot@mydomain.com. Write me!';

// $match[0] RETURNS ONLY THE VALID EMAIL ADDRESS
$match[0] == 'spam-a-lot@mydomain.com';

// $match[1] RETURNS ONLY THE USERNAME PART
$match[1] == 'spam-a-lot';

// $match[2] RETURNS ONLY THE DOMAIN NAME PART
$match[2] == 'mydomain.com';
?>

To match an email address, and replace it with a clickable link to the email address, use:

<?php
$ret = preg_replace("#([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i",
    "\\1<a href="mailto:\\2@\\3">\\2@\\3</a>", $input);

// $input - A STRING CONTAINING AN EMAIL ADDRESS
$input = 'My email address is spam-a-lot@mydomain.com. Write me!';

// $ret - RETURNED STRING CONTAINING A CLICKABLE EMAIL LINK
$ret == 'My email address is <a
    href="mailto:spam-a-lot@mydomain.com">spam-a-lot@mydomain.com</a>.
    Write me!';
?>

Leave a Reply

Your email address will not be published. Required fields are marked *