Home (http://www.catswhocode.com/blog) Snippets (http://www.catswhocode.com/blog/snippets)
About (http://www.catswhocode.com/blog/about) Contact (http://www.catswhocode.com/blog/contact) Categories
(http://www.catswhocode.com)
Hire me (http://www.webdevcat.com)
15 PHP regularexpressions for webdevelopers(http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers)Published on July 27th, 2009 by Jean-Baptiste Jung. 161 Comments
(http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-
developers#comments) -
Regular expressions are a very useful tool for developers.
They allow to find, identify or replace text, words or any kind
of characters. In this article, I have compiled 15+ extremely
useful regular expressions that any web developer should
have in his toolkit.
Getting started with regularexpressionsFor many beginners, regular expressions seems to be hard
to learn and use. In fact, they’re far less hard than you may
think. Before we dive deep inside regexp with useful and
reusable codes, let’s quickly see the basics:
Regular expressions syntax
Regular
Expression
Will match…
foo The string “foo”
foo “foo” at the start of a string
foo$ “foo” at the end of a string
foo$ “foo” when it is alone on a string
[abc] a, b, or c
(http://stats.buysellads.com/click.go?z=1238342&b=4260527&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.956685998942703&link=https://enoi.se/shared)
(http://stats.buysellads.com/click.go?z=1238342&b=4305008&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.11878540297038853&link=http://ghostlab.vanamco.com/?utm_source=www.catswhocode.com&utm_medium=ad-ID4305008&utm_content=&utm_campaign=buyandsell)
(http://stats.buysellads.com/click.go?z=1238342&b=4058484&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.43896794063039124&link=http://themebeans.com/?
ref=cwc_v2)
(http://stats.buysellads.com/click.go?z=1238342&b=3796057&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.469799846643582&link=http://www.kendoui.com/?utm_source=CatsWhoCode&utm_medium=banner&utm_campaign=BSA_KendoUI_CatsWhoCode_animated_HTML5_125x125)
(http://stats.buysellads.com/click.go?z=1238342&b=218431&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.08460462559014559&link=http://www.pagelines.com?utm_source=BuySellAds&utm_medium=Banner&utm_campaign=CatsWhoCode&utm_nooverride=1)
(http://stats.buysellads.com/click.go?z=1238342&b=4279869&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.7347113967407495&link=http://www.lunarpages.com/basic-
special/)
Advertise Here
19400+RSS Readers
(http://feeds.feedburner.com/catswhocode)
9300+Followers
(http://www.twitter.com/catswhocode)
[a-z] Any lowercase letter
[^A-Z] Any character that is not a uppercase
letter
(gif|jpg) Matches either “gif” or “jpeg”
[a-z]+ One or more lowercase letters
[0-9.-] Ðny number, dot, or minus sign
[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number
or _
([wx])([yz]) wy, wz, xy, or xz
[^A-Za-z0-9] Any symbol (not a number or a letter)
([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers
PHP regular expression functions
Function Description
preg_match() The preg_match() function searches
string for pattern, returning true if pattern
exists, and false otherwise.
preg_match_all() The preg_match_all() function matches
all occurrences of pattern in string.
preg_replace() The preg_replace() function operates just
like ereg_replace(), except that regular
expressions can be used in the pattern
and replacement input parameters.
preg_split() The preg_split() function operates exactly
like split(), except that regular
expressions are accepted as input
parameters for pattern.
preg_grep() The preg_grep() function searches all
elements of input_array, returning all
elements matching the regexp pattern.
preg_ quote() Quote regular expression characters
Validate domain nameVerify if a string is a valid domain name.
(http://stats.buysellads.com/click.go?z=1239978&b=4275658&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.34081513271667063&link=http://www.searchmarketinggroup.co.uk/services/technical-
audit/technical-audit-compliance)
(http://stats.buysellads.com/click.go?z=1239978&b=4270330&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.3209899279754609&link=http://coderaria.com/about/)
(http://stats.buysellads.com/click.go?z=1239978&b=2453300&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.36391320917755365&link=http://www.elegantthemes.com/gallery/divi/)
Advertise Here
(http://buysellads.com/buy/detail/3863/zone/1239978?
utm_source=site_3863&utm_medium=website&utm_campaign=adhere&utm_content=zone_1239978)
Like CWC on Facebook!
Cats Who Code
4,115 people like Cats Who Code.
Facebook social plugin
Like
Latest posts
10+ handy and reusable jQuery code snippets
(http://www.catswhocode.com/blog/10-handy-
and-reusable-jquery-code-snippets)
3 one-year subscriptions giveaway from IM
Creator (http://www.catswhocode.com/blog/3-
one-year-subscriptions-giveaway-from-im-
creator)
Top 10 PHP frameworks for 2014
$url = "http://komunitasweb.com/";
if (preg_match('/(http|https|ftp)://([A-Z0-9
][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?
/?/i', $url)) {
echo "Your url is ok.";
} else {
echo "Wrong url.";
}
» Source (http://snipplr.com/view/14198/useful-regex-
functions/)
Enlight a word from a textThis very useful regular expression find a specific word in a
text, and enlight it. Extremely useful for search results.
$text = "Sample sentence from KomunitasWeb, r
egex has become popular in web programming. N
ow we learn regex. According to wikipedia, Re
gular expressions (abbreviated as regex or re
gexp, with plural forms regexes, regexps, or
regexen) are written in a formal language tha
t can be interpreted by a regular expression
processor";
$text = preg_replace("/b(regex)b/i", '<span s
tyle="background:#5fc9f6">1</span>', $text);
echo $text;
» Source (http://snipplr.com/view/14198/useful-regex-
functions/)
Enlight search results in yourWordPress blogAs I just said that the previous code snippet could be very
handy on search results, here is a great way to implement it
on a WordPress blog.
Open your search.php file and find the the_title() function.
Replace it with the following:
echo $title;
Now, just before the modified line, add this code:
Search
(http://www.catswhocode.com/blog/top-10-php-
frameworks-for-2014)
Best WordPress snippets, hacks and tips from
2013 (http://www.catswhocode.com/blog/best-
wordpress-snippets-hacks-and-tips-from-2013)
Christmas giveaway: $500 worth of services
from CodeInWP
(http://www.catswhocode.com/blog/christmas-
giveaway-500-worth-of-services-from-codeinwp)
WP theme of the week
(http://t.co/lPhxz2fk7l) Lania
(http://t.co/lPhxz2fk7l)
CatsWhoCode.com is hosted by VidaHost. Use our
exclusive coupon CATSWHOCODE to get a 10%
discount on hosting! (http://is.gd/vidahostcwc)
Search
(http://feeds.feedburner.com/catswhocode-
snippets) Latest snippets
(http://www.catswhocode.com/blog)
WordPress All-in-one Contact Form
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/D0PcVtauk6Q/wordpress-all-in-one-
contact-form)
One file post views counter script
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/12qWs-1G0kE/one-file-post-views-
counter-script)
Activate WordPress link manager
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/tVb4eK51fA8/activate-wordpress-
link-manager)
<?php
$title = get_the_title();
$keys= explode(" ",$s);
$title = preg_replace('/('.implode('
|', $keys) .')/iu',
'<strong class="search-excerp
t">\0</strong>',
$title);
?>
Save the search.php file and open style.css. Append the
following line to it:
strong.search-excerpt { background: yellow; }
» Source (http://yoast.com/wordpress-search/)
Get all images from a HTMLdocumentIf you ever widhed to be able to get all images form a
webpage, this code is a must have for you. You should
easily create an image downloader using the power of cURL
(http://www.catswhocode.com/blog/10-awesome-
things-to-do-with-curl).
$images = array();
preg_match_all('/(img|src)=("|')["'>]+/i', $
data, $media);
unset($data);
$data=preg_replace('/(img|src)("|'|="|=')(.*)
/i',"$3",$media[0]);
foreach($data as $url)
{
$info = pathinfo($url);
if (isset($info['extension']))
{
if (($info['extension'] == 'j
pg') ||
($info['extension'] == 'jpeg'
) ||
($info['extension'] == 'gif')
||
($info['extension'] == 'png')
)
array_push($images, $url);
}
}
Disable autocomplete and autocorrect in HTML
forms
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/QMmJjQuvab8/disable-
autocomplete-and-autocorrect-in-html-forms)
No Follow Links In Specific Category
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/4MIGwUMNaZ4/no-follow-links-in-
specific-category)
Insert line breaks in HTML buttons
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/kia2mZ8bbHQ/insert-line-breaks-in-
html-buttons)
Count user posts on a WordPress site
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/2gnlEq24g9c/count-user-posts-on-a-
wordpress-site)
Highlight current menu item in PHP
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/vqkotB-TEOc/highlight-current-
menu-item-in-php)
Language Dependent Float
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/vVcAwE_XTuk/language-
dependent-float)
Bookmark (aka: Link) Shortcode
(http://feedproxy.google.com/~r/catswhocode-
snippets/~3/ECGVV6hZtjc/bookmark-aka-link-
shortcode)
Blogroll
Custom Logo Design (http://www.logobee.com/)
Custom Logo Design Serv ices
(http://www.LogoDesignGeek.com)
festklänningar (http://www.amormoda.se/Festklanningar-
c20/)
Free Website Templates by Wix
(http://www.wix.com/website/templates)
Logo Design Serv ice (http://www.LogoNerds.com)
Rencontre Végétarien (http://v egan-rencontres.fr/)
Stock Photos (http://www.123rf.com)
Visiter le Québec (http://www.v isiter-le-quebec.com/)
Visiter New York (http://www.v isiter-newyork.com)
Web Hosting (http://www.inmotionhosting.com)
WP CAT – We fix WordPress blogs (http://wpcat.com/)
» Source
(http://davebrooks.wordpress.com/2009/04/22/php-
preg_replace-some-useful-regular-expressions/)
Remove repeated words(case insensitive)Often repeating words while typing? This handy regexp will
be very helpful.
$text = preg_replace("/s(w+s)1/i", "$1", $tex
t);
» Source
(http://davebrooks.wordpress.com/2009/04/22/php-
preg_replace-some-useful-regular-expressions/)
Remove repeatedpunctuationSame as above, but with punctuation. Goodbye repeated
commas!
$text = preg_replace("/.+/i", ".", $text);
» Source
(http://davebrooks.wordpress.com/2009/04/22/php-
preg_replace-some-useful-regular-expressions/)
Matching a XML/HTML tagThis simple function takes two arguments: The first is the
tag you’d like to match, and the second is the variable
containing the XML or HTML. Once again, this can be very
powerful used along with cURL
(http://www.catswhocode.com/blog/10-awesome-
things-to-do-with-curl).
function get_tag( $tag, $xml ) {
$tag = preg_quote($tag);
preg_match_all('{<'.$tag.'[>]*>(.*?)</'.$t
ag.'>.'}',
$xml,
$matches,
PREG_PATTERN_ORDER);
return $matches[1];
}
» Source (http://immike.net/blog/2007/04/06/5-regular-
expressions-every-web-programmer-should-know/)
Matching an XHTML/XML tagwith a certain attribute valueThis function is very similar to the previous one, but it allow
you to match a tag having a specific attribute. For example,
you could easily match <div id=”header”>.
function get_tag( $attr, $value, $xml, $tag=n
ull ) {
if( is_null($tag) )
$tag = '\w+';
else
$tag = preg_quote($tag);
$attr = preg_quote($attr);
$value = preg_quote($value);
$tag_regex = "/<(".$tag.")[>]*$attr\s*=\s*
".
"(['\"])$value\\2[>]*>(.*?)<
\/\\1>/"
preg_match_all($tag_regex,
$xml,
$matches,
PREG_PATTERN_ORDER);
return $matches[3];
}
» Source (http://immike.net/blog/2007/04/06/5-regular-
expressions-every-web-programmer-should-know/)
Matching hexadecimal colorvalues
Another interesting tool for web developers! It allows you to
match/validate a hexadecimal color value.
$string = "#555555";
if (preg_match('/#(?:(?:[a-fd]{3}){1,2})$/i'
, $string)) {
echo "example 6 successful.";
}
» Source (http://www.talkphp.com/advanced-php-
programming/1612-8-practical-php-regular-
expressions.html)
Find page titleThis handy code snippet will find and print the text within the
<title> and </title> tags of a html page.
$fp = fopen("http://www.catswhocode.com/blog"
,"r");
while (!feof($fp) ){
$page .= fgets($fp, 4096);
}
$titre = eregi("<title>(.*)</title>",$page,$r
egs);
echo $regs[1];
fclose($fp);
Parsing Apache logsMost websites are running on the well-known Apache
webserver. If your website does, what about using PHP and
some regular expressions to parse Apache logs?
//Logs: Apache web server
//Successful hits to HTML files only. Useful
for counting the number of page views.
'((?#client IP or domain name)S+)s+((?#basic
authentication)S+s+S+)s+[((?#date and time)[
]]+)]s+"(?:GET|POST|HEAD) ((?#file)/[ ?"]+?
.html?)??((?#parameters)[ ?"]+)? HTTP/[0-9.]
+"s+(?#status code)200s+((?#bytes transferred
)[-0-9]+)s+"((?#referrer)["]*)"s+"((?#user a
gent)["]*)"$'
//Logs: Apache web server
//404 errors only
'((?#client IP or domain name)S+)s+((?#basic
authentication)S+s+S+)s+[((?#date and time)[
]]+)]s+"(?:GET|POST|HEAD) ((?#file)[ ?"]+)?
?((?#parameters)[ ?"]+)? HTTP/[0-9.]+"s+(?#s
tatus code)404s+((?#bytes transferred)[-0-9]+
)s+"((?#referrer)["]*)"s+"((?#user agent)["
]*)"$'
» Source
(http://www.roscripts.com/PHP_regular_expressions_examples-
136.html)
Replacing double quotes bysmart qutotesIf you’re a typographer lover, you’ll probably love this regexp,
which allow you to replace normal double quotes by smart
quotes. A regular expression of that kind is used by
WordPress on contents.
preg_replace('B"b(["x84x93x94rn]+)b"B', '?1?
', $text);
» Source
(http://www.roscripts.com/PHP_regular_expressions_examples-
136.html)
Checking passwordcomplexityThis regular expression will tests if the input consists of 6 or
more letters, digits, underscores and hyphens.
The input must contain at least one upper case letter, one
lower case letter and one digit.
'A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[
a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{6,
}z'
» Source
(http://www.roscripts.com/PHP_regular_expressions_examples-
136.html)
WordPress: Using regexp toretrieve images from postAs I know many of you are WordPress users, you’ll probably
enjoy that code which allow you to retrieve all images from
post content and display it.
To use this code on your blog, simply paste the following
code on one of your theme files.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$szPostContent = $post->post_content;
$szSearchPattern = '~<img [>]* />~';
// Run preg_match_all to grab all the images
and save the results in $aPics
preg_match_all( $szSearchPattern, $szPostCont
ent, $aPics );
// Check to see if we have at least 1 image
$iNumberOfPics = count($aPics[0]);
if ( $iNumberOfPics > 0 ) {
// Now here you would do whatever you ne
ed to do with the images
// For this example the images are just
displayed
for ( $i=0; $i < $iNumberOfPics ; $i++ )
{
echo $aPics[0][$i];
};
};
endwhile;
endif;
?>
» Source (http://www.wprecipes.com/how-to-retrieve-
images-in-post-content)
Generating automatic
Tweet 109 97
Like
23
0
More posts about PHP
Top 10 PHP frameworks for 2014 (http://www.catswhocode.com/blog/top-10-php-
frameworks-for-2014)
7 little known but super useful PHP functions
(http://www.catswhocode.com/blog/7-little-known-but-super-useful-php-functions)
Useful snippets for php dev elopers (http://www.catswhocode.com/blog/useful-
snippets-for-php-dev elopers)
Awesome tutorials to master CodeIgniter
(http://www.catswhocode.com/blog/awesome-tutorials-to-master-codeigniter)
10 awesome PHP functions and snippets (http://www.catswhocode.com/blog/10-
awesome-php-functions-and-snippets)
How to create a simple and efficient PHP cache
(http://www.catswhocode.com/blog/how-to-create-a-simple-and-efficient-php-
cache)
Share this article
(http://stats.buysellads.com/click.go?z=1244474&b=4065164&g=&s=&sw=1366&sh=768&br=chrome,32,win&r=0.009492592886090279&link=http://www.templatemonster.com/wordpress-
themes.php?utm_source=catswhocode&utm_medium=banner&utm_campaign=bsaseptember)
Generating automaticsmileysAnother function used by WordPress, this one allow you to
automatically replace a smiley symbol by an image.
$texte='A text with a smiley :-)';
echo str_replace(':-)','<img src="smileys/sou
riant.png">',$texte);
Comments (161) - Leave yours
(#reply-title)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36741#respond)
Henry@Nonfault claims
(http://www.easigo.co.uk) said: July 27,
2009 at 7:00 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36741)
Just wanna say awesome post. The whole
PHP content you showed above really help
most of the web developers and help to make
their task easy. Me too very thankful to you
for this nice post..
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36742#respond)
Will Anderson
(http://www.itsananderson.com/) said: July
27, 2009 at 7:09 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36742)
Hey, I haven’t tested to make sure, but I think
the first get_tag function would be broken by
the following:
get_tag('a', '<a title="is a > b?"
href="a-gt-b.htm" />Is A > B?</a>');
The RegEx would match that first > in the
attribute and the following text would be
returned
b?” href=”a-gt-b.htm” />Is A > B?
OK, hopefully none of this comment will be
interpreted by WordPress in an unexpected
way
housed said: July 27, 2009 at 7:22 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36744)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36744#respond)
Hello,
I don’t undertand how they work. For example
if i want that $test don’t have any uppercase
letter, how I have to do?
Reply (/blog/15-php-regular-
expressions-for-web-
developers?
replytocom=787218#respond)
asdf (http://asdf.com) said: May
10, 2012 at 12:56 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-
web-developers#comment-
787218)
strtolower($test);
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36748#respond)
15 PHP regular expressions for web
developers | Webs Developer
(http://www.websdeveloper.com/15-php-
regular-expressions-for-web-developers/)
said: July 27, 2009 at 8:02 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36748)
[...] 15 PHP regular expressions for web
developers Share and Enjoy: [...]
Reply (/blog/15-php-regular-expressions-
Alex (http://alexshapovalov.com/) said:
July 27, 2009 at 8:50 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36754)
first I would like to say, “great article”. I
haven’t used RegEx in quite sometime now,
but if I remember correctly “[0-9.-] Ðny
number, dot, or minus sign” in this example
the dot will match anything, if you would like
match the dot you have to escape it. please
correct me if I am wrong.
for-web-developers?
replytocom=36754#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36756#respond)
payam (http://webnevis.net) said: July 27,
2009 at 9:15 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36756)
very nice
thanks
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36761#respond)
Jake Rocheleau
(http://www.insidethewebb.com/) said: July
27, 2009 at 10:11 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36761)
Sweet article! I’ve seen a lot of Cats Who
Code around the web recently, keep up the
great posts and I’ll be following along all the
way.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36763#respond)
Wheelchair Guide
(http://www.wheelchairguide.net) said:
July 27, 2009 at 10:13 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36763)
I am a big fan of regular expressions,
because they can make sorting through a
bunch of text so much simpler. I wish I had
known about them when I first started coding.
This is a cool guide and has some great
examples!
15 PHP regular expressions for web
developers « Netcrema - creme de la social
news via digg + delicious + stumpleupon
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36773#respond)
+ reddit (http://www.netcrema.com/?
p=8762) said: July 27, 2009 at 11:35 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36773)
[...] 15 PHP regular expressions for web
developerscatswhocode.com [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36778#respond)
Peter C. (http://cowburn.info) said: July 28,
2009 at 12:10 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36778)
Seems WP has munged up your code
(particularly backslash escaping).
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36781#respond)
15 PHP regular expressions for web
developers (http://sovit.biz/15-php-regular-
expressions-for-web-developers/) said: July
28, 2009 at 12:37 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36781)
[...] original here: 15 PHP regular expressions
for web developers SHARETHIS.addEntry({
title: "15 PHP regular expressions for web
developers", url: [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36785#respond)
Fred P. said: July 28, 2009 at 1:27 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36785)
You mean:
$text = preg_replace(“/\\.+/i”, “.”, $text);
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36788#respond)
Samuel Folkes
(http://www.samuelfolkes.com) said: July
28, 2009 at 1:37 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36788)
Excellent article, I found the code to parse
Apache logs and to get all images in a HTML
document particularly clever. You did however
leave out a very common use for regex that
web developers often need to use: validating
email addresses.
$address = 'some email address here';
$ valid = preg_match('/(?:
[\w\!\#\$\%\&\'\*\+\-\/\=\?\\\
{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-
\/\=\?\\\{\|\}\~]+@(?:(?:(?:[a-zA-
Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}
[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-
zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-
9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-
4]\d|25[0-5])\.){3}(?:[01]?
\d{1,2}|2[0-4]\d|25[0-5])\]))$/',
$address);
echo $valid;
The preg_match() function above will of
course evaluate to false. Again, excellent
article. I will be putting some of these to use
in the near future no doubt.
Reply (/blog/15-php-regular-
expressions-for-web-
developers?
Siddhesh (http://www.news-
library.com) said: May 3, 2010 at
3:24 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-
web-developers#comment-
80853)
Thanks for such nice regex.
But can I get an regex which will
replace text which is enclosed in
html comment.
replytocom=80853#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36795#respond)
Sumesh (http://designblurb.com) said:
July 28, 2009 at 3:32 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36795)
That’s a useful list, I recently watched the
Themeforest screencast on regex too.
And to anyone who gets trouble when posting
code, just escape them using an encoder like
Postable.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36796#respond)
Toby said: July 28, 2009 at 3:40 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36796)
Enlight != word. I’d suggest ‘highlight’
instead.
@#3 – if you want to make sure that you
have only lowercase chars in your string
$test, do
if(preg_match(“/[a-z]/”,$test))
{
echo ‘all lowercase’;
}
Toby said: July 28, 2009 at 3:41 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36797)
oops – I see that my five second regex
doesn’t quite do what you’re asking for –
make that
if(!preg_match(â€/[A-Z]/â€,$test))
{
echo ‘all lowercase’;
}
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36797#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36800#respond)
links for 2009-07-27 .:: [aka Ñ
‰Ñмукюшт] Ozver.in | ОзвеÑ
€Ð¸Ð½ (http://ozver.in/2009/07/28/links-for-
2009-07-27/) said: July 28, 2009 at 5:04 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36800)
[...] 15 PHP regular expressions for web
developers (tags: php regex programming tips
regexp wordpress code regular) [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36817#respond)
15 PHP regular expressions for web
developers | Ajaxonomy
(http://www.ajaxonomy.com/2009/php/15-
php-regular-expressions-for-web-
developers) said: July 28, 2009 at 7:46 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36817)
[...] can read the full post here.
ShareCloseSocial WebE-mail [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36819#respond)
15 PHP regular expressions for web
developers « ãã£ã¦ã‚‚ï¼ã€€ã¡ã‚…
ã©ã‚“(雑記帳)
(http://tyudon.com/blog/?p=1258) said:
July 28, 2009 at 7:51 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36819)
[...] 15 PHP regular expressions for web
developers 15 PHP regular expressions for
web developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36838#respond)
Fred (http://frederic-soler.fr) said: July 28,
2009 at 10:33 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36838)
Merci Jean, c’est complet et et très utile.
Bye.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36839#respond)
Regular Expression for PHP Code – Daily
interesting stuffs
(http://daily.abifathir.com/2009/07/regular-
expression-for-php-code/) said: July 28,
2009 at 10:42 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36839)
[...] Expression is a powerfull tools to
manipulate text string. Today I found the 15
example of regex usage in PHP. The
examples in that post is something that I
looking for all the time. I’m lazy enough to
[...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36846#respond)
Yosh said: July 28, 2009 at 11:17 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36846)
You forgot to escape dots in some of your
expressions.
A dot is a meta-character to match all
characters except newline.
Billyboylindien
(http://www.billyboylindien.com) said: July
28, 2009 at 11:56 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36851)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36851#respond)
Thanks a lot for these tips.
Very useful.
Bookmarked
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36879#respond)
CatsWhoCode.com: 15 PHP regular
expressions for web developers -
Kerkness.ca
(http://www.kerkness.ca/catswhocodecom-
15-php-regular-expressions-for-web-
developers/) said: July 28, 2009 at 5:14 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36879)
[...] CatsWhoCode.com today there’s a new
article listing fifteen handy regular
expressions you might find useful in your
day-to-day development (as [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36889#respond)
CatsWhoCode.com: 15 PHP regular
expressions for web developers | PHP
(http://www.dreamnest.in/php/catswhocode-
com-15-php-regular-expressions-for-web-
developers.html) said: July 28, 2009 at 6:02
pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36889)
[...] CatsWhoCode.com today there’s a new
article listing fifteen handy regular
expressions you might find useful in your
day-to-day development (as [...]
CatsWhoCode.com: 15 PHP regular
expressions for web developers | Webs
Developer
(http://www.websdeveloper.com/catswhocodecom-
15-php-regular-expressions-for-web-
developers/) said: July 28, 2009 at 6:03 pm
(http://www.catswhocode.com/blog/15-
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36890#respond)
php-regular-expressions-for-web-
developers#comment-36890)
[...] CatsWhoCode.com today there’s a new
article listing fifteen handy regular
expressions you might find useful in your
day-to-day development (as [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36899#respond)
Internet Brain » 15 PHP regular
expressions for web developers
(http://imaginaryworld.net/2009/07/28/15-
php-regular-expressions-for-web-
developers/) said: July 28, 2009 at 7:12 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36899)
[...] 15 PHP regular expressions for web
developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36903#respond)
John Turner (http://johndturner.com) said:
July 28, 2009 at 7:49 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36903)
This is an awesome online regex tester. I use
it all the time.
http://www.spaweditor.com/scripts/regex/index.php
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36904#respond)
Jedaffra said: July 28, 2009 at 7:50 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36904)
What do you mean by “enlight” – is that like,
highlight?
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36911#respond)
All Teched Up « Caintech.co.uk
(http://caintechnews.wordpress.com/2009/07/28/all-
teched-up-6/) said: July 28, 2009 at 8:55
pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36911)
[...] CSS-Tricks 7 Secrets to Tweeting Your
Corporate Culture How to Buy a Mattress *
Get Rich Slowly 15 PHP regular expressions
for web developers E-Commerce Web Design
Toolbox 22 Free Fonts To Achieve That Hand
Drawn Effect Web Design Tutori [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36916#respond)
Monica (http://www.monica2000.com)
said: July 28, 2009 at 9:29 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36916)
Thanks a lot! Very useful tips and apps! I’ve
tried a few already and can’t be more happier.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36954#respond)
links for 2009-07-28 | Jnbn.Org
(http://www.jnbn.org/free-time/links-for-
2009-07-28-90.html) said: July 29, 2009 at
2:37 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36954)
[...] 15 PHP regular expressions for web
developers (tags: regular-expressions
web_development programacion tools tutorial
php code programming development tutorials
scripts tips regex expression regexp
regularexpression expressions regular
examples snippets) [...]
Jestep (http://www.saynotoflash.com/)
said: July 29, 2009 at 2:53 am
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36956#respond)
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36956)
Great set of regex’s. Another good one to add
would be a decent phone number validation,
especially for international #’s, and maybe a
clean credit card number validator.
Anyway, good resource.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36957#respond)
Goonanism » links for 2009-07-28
(http://goonanism.com/blog/2009/07/29/links-
for-2009-07-28/) said: July 29, 2009 at 3:05
am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36957)
[...] 15 PHP regular expressions for web
developers (tags: webdesign javascript php
tutorials) [...]
Juan Dela Cruz (http://dev.pasukan.com)
said: July 29, 2009 at 3:07 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36958)
Validate domain name is not working.
Anyone want to correct this regular
expression?
Error: Found:
Warning: preg_match() [function.preg-match]:
Unknown modifier ‘/’ in D:\projects\regex.php
on line 3
Wrong url.
$url = “http://komunitasweb.com/”;
if (preg_match(‘/ (http|https|ftp)://([A-Z0-9][A-
Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i’,
$url)) {
echo “Your url is ok.”;
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36958#respond)
} else {
echo “Wrong url.”;
}
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36963#respond)
Waynes Weekly Notable Links | The Bliss
of Being
(http://blissofbeing.com/blog/2009/07/28/waynes-
weekly-notable-links-6/) said: July 29, 2009
at 3:33 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36963)
[...] Shared 15 PHP regular expressions for
web developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=36999#respond)
Tom Stone (http://blogg.tomstone.se) said:
July 29, 2009 at 8:47 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-36999)
Regarding the example “Validate domain
name” – does that match IDN
(Internationalized Domain Name) domains
also? Like: www.åäö.se
Reply (/blog/15-php-regular-expressions-
Teddy (http://ww.jasin.biz) said: July 29,
2009 at 9:22 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37003)
Good post!….
would like to add the following ExtJS regexp
form validation that you can simply use for
PHP regexp as well
http://extjs.com/forum/showthread.php?
t=4271
for-web-developers?
replytocom=37003#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37006#respond)
Francois Brill
(http://www.creativeink.co.za) said: July
29, 2009 at 9:59 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37006)
Nice Post! Thanks
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37008#respond)
Phil Cook (http://phil-cook.com) said: July
29, 2009 at 10:02 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37008)
Hi. Just wanted to say that this is an
excellent post. I needed someone to explain
clearly the meaning of symbols for reg
expressions and this does it with ease.
Bookmarked and I will be subscribing to the
RSS. Many thanks.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37011#respond)
candra (http://www.candra.web.id) said:
July 29, 2009 at 10:15 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37011)
I love this post…. very useful thx
15 expresiones regulares en PHP para
desarrolladores - tikitak-o-rama
(http://tikitakorama.com.ar/archivo/15-
expresiones-regulares-en-php-para-
desarrolladores/) said: July 29, 2009 at
10:35 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37013#respond)
developers#comment-37013)
[...] a un RT de Smashing Magazine llego a
una genial lista de 15 expresiones regulares
en PHP útiles para desarrolladores en la
que se pueden encontrar desde un validador
de URI hasta una expresión para parsear
logs de [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37020#respond)
aldrin (http://www.aldrinponce.com) said:
July 29, 2009 at 11:08 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37020)
very great post! anyway this is very useful in
some projects, specially for web scrap I
use to work most this things for a year but
suddenly I getting bored..
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37032#respond)
15 PHP regular expressions for web
developers « Illuminations
(http://dmilluminations.wordpress.com/2009/07/29/15-
php-regular-expressions-for-web-
developers/) said: July 29, 2009 at 12:57
pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37032)
[...] 15 PHP regular expressions for web
developers. Tags: [...]
Reply (/blog/15-php-regular-expressions-
Bilge said: July 29, 2009 at 1:12 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37036)
Super cool story, bro. None of these
examples actually work since all
backslashes have been stripped.
for-web-developers?
replytocom=37036#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37037#respond)
Boris Momcilovic (http://kornrunner.net)
said: July 29, 2009 at 1:16 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37037)
Following one is wrong:
Remove repeated punctuation
Same as above, but with punctuation.
Goodbye repeated commas!
$text = preg_replace(“/.+/i”, “.”, $text);
You’re replacing any number of any not a line
break character with a dot. If you wished to
replace dots, you must escape it first like:
$text = preg_replace(“/\.+/i”, “.”, $text);
Or if you wanted to replace multiple commas,
put commas (no escaping needed).
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37041#respond)
Michael Doyle’s Blog » PHP - Regular
Expressions (http://michaeldoyle.eu/blog/?
p=220) said: July 29, 2009 at 1:47 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37041)
[...] http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-developers
[...]
15 PHP Regular Expressions for Web
Developers | Choose Daily
(http://choosedaily.com/503/15-php-
regular-expressions-web-developers/) said:
July 29, 2009 at 2:18 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37048)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37048#respond)
[...] 15 PHP Regular Expressions for Web
Developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37063#respond)
Chris Jean (http://gaarai.com/) said: July
29, 2009 at 3:56 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37063)
It looks like a number of the regex strings
were modified when you copied and pasted
them. Primarily, the backslashes were
dropped. For example, you have “/s(w+s)1/i”
for the remove repeated words regex when it
should be “/\s(\w+\s)\1/i”. Repeated
punctuation, hexadecimal color values, smart
quotes, password complexity, and probably
some others that I missed all have this
problem.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37084#respond)
Jim Hanifen (http://www.jimhanifen.com)
said: July 29, 2009 at 8:13 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37084)
Love seeing a post not just about regular
expressions, but actual usage and examples.
Thanks for the post.
15 PHP regular expressions for web
developers « HUGO TORRES
(http://hugotorres.wordpress.com/2009/07/29/15-
php-regular-expressions-for-web-
developers/) said: July 29, 2009 at 8:36 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37085)
[...] Julio 29, 2009 15 PHP regular
expressions for web developers. [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37085#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37104#respond)
David Rios (dfrios) 's status on Wednesday,
29-Jul-09 20:56:35 UTC - Identi.ca
(http://identi.ca/notice/7223119) said: July
29, 2009 at 10:56 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37104)
[...] 15 PHP regular expressions for web
developers:
http://www.catswhocode.com/blog/15-php-
regular-expressions-for-web-developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37120#respond)
Sahus Pilwal
(http://www.sweetpixels.co.uk) said: July
30, 2009 at 1:38 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37120)
Agree great post for the simple fact you post
snippets of code and use real world
examples. The replace smiley exmaple is the
most useful I think!? Thanks for the
information!
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37191#respond)
Regular Expression to Get All Image URLs
From a Page (http://www.webmaster-
source.com/2009/07/30/regular-expression-
to-get-all-image-urls-from-a-page/) said:
July 30, 2009 at 2:00 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37191)
[...] Source: 15 PHP regular expressions for
web developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37461#respond)
cole said: July 31, 2009 at 3:34 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37461)
Enlight…highlight…what’s the difference. I’m
just a stupid American and I knew what he
meant. I’m pretty sure that if I had to, I would
have chosen the wrong french word, or tense,
or gender…whatever. Good post – simplest,
most useful summary of regex I’ve ever read.
Thanks.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37486#respond)
عبور » Blog Archive » عباØ
±Ø§Øª باقاعده
(http://www.aazamzandi.com/?p=85) said:
July 31, 2009 at 6:19 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37486)
[...] بین تویترگردىâ
€ŒÙ‡Ø§Ù‰ امروز این
لینک رو پیدا کردم
که برام خیلى جالب
بود .  این عبارات [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37492#respond)
Indika (http://Indika.info) said: July 31,
2009 at 8:25 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37492)
Thanks for the great article.
PHP regular expressions for web
developers | Â Â Indika's Web Log
(http://indika.info/2009/07/31/php-regular-
expressions-for-web-developers/) said: July
31, 2009 at 8:28 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37493#respond)
developers#comment-37493)
[...] Here is the complete article [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37526#respond)
Mattias said: August 1, 2009 at 2:32 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37526)
Can you please go through the expression for
Validate domain name?
I also got the error =
Warning: preg_match() [function.preg-match]:
Unknown modifier ‘/’
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37527#respond)
CurtainDog said: August 1, 2009 at 2:58 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37527)
Matching XML tags won’t work if you have
similar nested tags. XML in general is quite
evil.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37528#respond)
lkkreplica (http://www.lkkreplica.com)
said: August 1, 2009 at 3:05 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37528)
Regular expressions make it more easy for
the developers!
Maxx Buwaya
(http://royalvacationstravel.com) said:
August 1, 2009 at 11:11 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37567#respond)
developers#comment-37567)
Thanks a lot! Very useful tips and apps! Iâ
€™ve tried a few already and can’t be
more happier.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37573#respond)
BlogBuzz August 1, 2009
(http://www.webmaster-
source.com/2009/08/01/blogbuzz-august-1-
2009/) said: August 1, 2009 at 12:14 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37573)
[...] 15 PHP regular expressions for web
developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37603#respond)
Zecc said: August 1, 2009 at 7:54 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37603)
In your domain name validation, you got
something like this: (d+)?
I assume it’s meant to validate a port
number?
It should be instead be like this: (:\d+)
Otherwise it would match a single colon
without a number following it.
Also note there was a backslash missing
before the d.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
Diego (http://criterioweb.com) said: August
1, 2009 at 9:09 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37608)
Very Usefull! Thanks
replytocom=37608#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37649#respond)
Epic Alex (http://epicalex.com/) said:
August 2, 2009 at 1:59 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37649)
Fantastic article. I was wondering though,
any ideas why a preg_replace might not work
with the_excerpt, and what could I do to
make it work?
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=37827#respond)
WebDev Weekly #31 | ASAP - As Simple
as Possible
(http://samuli.hakoniemi.net/blog/webdev-
weekly-31/) said: August 2, 2009 at 10:54
pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-37827)
[...] 15 PHP regular expressions for web
developersA nice tutorial about useful regular
expressions to be used in PHP. [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38019#respond)
Syntaxerror (http://www.php-meets-
seo.com) said: August 4, 2009 at 12:29 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38019)
Cool – thanks for yout work.
15 PHP regular expressions for web
developers « I.T News & Stuff
(http://orange.id.au/wordpress/2009/08/04/15-
php-regular-expressions-for-web-
developers/) said: August 4, 2009 at 1:20
am
(http://www.catswhocode.com/blog/15-
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38025#respond)
php-regular-expressions-for-web-
developers#comment-38025)
[...] Link [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38191#respond)
17 PHP Practices That Should Be Banished
Forever
(http://www.samuelfolkes.com/2009/08/17-
php-practices-that-should-be-banished-
forever/) said: August 4, 2009 at 7:00 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38191)
[...] tool which when used correctly, can
accomplish some pretty amazing things as
demonstrated in this excellent article.
However, they are far slower than the native
PHP string manipulation functions and tend
to make [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38338#respond)
Best of the Web- July | Web Tutorials
(http://www.web-tuts.com/best-of-the-web-
july/) said: August 5, 2009 at 1:20 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38338)
[...] Visit Article [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
Milch (http://www.reviewsarena.com) said:
August 5, 2009 at 2:25 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38354)
Hi! Pretty nice list of regular expressions.
Could be useful. Will check again when i will
need them. I also check your blog and there
are a lot of useful posts.
replytocom=38354#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38413#respond)
Best of the Web- July - Programming Blog
(http://www.neurosoftware.ro/programming-
blog/blogposter/web-resources/best-of-the-
web-july/) said: August 6, 2009 at 1:44 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38413)
[...] Visit Article [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38414#respond)
Best of the Web- July - Programming Blog
(http://www.neurosoftware.ro/programming-
blog/blogposter/web-resources/best-of-the-
web-july/) said: August 6, 2009 at 1:44 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38414)
[...] Visit Article [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38433#respond)
Best of the Web- July « Webby Tutos
(http://www.webbytutos.com/?p=15) said:
August 6, 2009 at 6:16 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38433)
[...] Visit Article [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
Best of the Web- July | Webby Tutos
(http://www.webbytutos.com/?p=28) said:
August 6, 2009 at 6:34 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38434)
[...] Visit Article [...]
replytocom=38434#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38465#respond)
Zamshed Farhan
(http://www.zamshed.info) said: August 6,
2009 at 11:35 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38465)
I am really surpried by getting this collections
of regular expressions, It’s really very helpful.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38561#respond)
Expressões Regulares para
Desenvolvedores WEB | WEB COM
FARINHA
(http://webcomfarinha.com/blog/?p=381)
said: August 6, 2009 at 8:44 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38561)
[...] http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-developers
[...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=38607#respond)
Best of the Web- July : Webby Tutos
(http://www.webbytutos.com/?p=66) said:
August 7, 2009 at 1:05 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-38607)
[...] Visit Article [...]
Daily Digest for 2009-08-07 | Pedro
Trindade
(http://pedrotrindade.com/2009/08/07/daily-
digest-for-2009-08-07/) said: August 8, 2009
at 9:03 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=39060#respond)
developers#comment-39060)
[...] 15 PHP regular expressions for web
developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=39373#respond)
15 PHP regular expressions for web
developers « gdhanasekar
(http://myweblogz.com/gdhanasekar/2009/08/10/15-
php-regular-expressions-for-web-
developers/) said: August 10, 2009 at 8:38
am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-39373)
[...] View More.. [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=39375#respond)
Pavel Golovko (http://www.astanda.com)
said: August 10, 2009 at 8:52 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-39375)
Validate domain name regular expression:
(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.))
{0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-
Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:
[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?
\d{1,2}|2[0-4]\d|25[0-5])\]))
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=39398#respond)
TxRx said: August 10, 2009 at 1:08 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-39398)
Superb post, bookmarked for sure !
Nice one!
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=39460#respond)
joyoge designers' bookmark
(http://www.joyoge.com/) said: August 10,
2009 at 7:12 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-39460)
helpful codes, thanks for the list..
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=39582#respond)
Raju (http://techpp.com/) said: August 11,
2009 at 5:46 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-39582)
How much ever I try, they just don’t seem to
get into my head
will bookmark and read again
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=39717#respond)
Ed R (http://www.behindthedot.net) said:
August 12, 2009 at 12:22 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-39717)
Thanks for this post. It’s surprising that many
developers don’t really understand the full
potential of regular expressions. They write
elaborate functions to extract items of data,
which can be done with a one line RE!!!
Reply (/blog/15-php-regular-expressions-
for-web-developers?
Best of the Web- July :: CSS ::
WEBDESIGN FAN
(http://webdesignfan.co.cc/?p=147) said:
August 13, 2009 at 2:39 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-39924)
[...] Visit Article [...]
replytocom=39924#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=40498#respond)
15 Very Useful PHP Tutorials
(http://www.listfied.com/15-very-useful-
php-tutorials) said: August 17, 2009 at 3:41
pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-40498)
[...] 13. 15 PHP regular expressions for web
developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=40623#respond)
Mark (http://www.webhost-guru.com) said:
August 18, 2009 at 11:59 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-40623)
Thanks, a really great article. I already
bookmarked this page.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=40653#respond)
Mark (http://www.9636.org) said: August
18, 2009 at 5:06 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-40653)
thanks for your information.it is very helpful for
me!
thanks
15 PHP regular expressions for web
developers « Alicia Wilkerson
(http://alicia.wilkersons.us/?p=789) said:
August 19, 2009 at 5:11 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-40789)
[...] 15 PHP regular expressions for web
developers. [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=40789#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=40884#respond)
# 15 PHP regular expressions for web
developers | AbhinavZone
(http://abhinavzone.com/15-php-regular-
expressions-for-web-developers/) said:
August 20, 2009 at 9:41 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-40884)
[...] I have found most comprehensive regular
expression on the web. I am sure that It will
help you  Read More [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=41337#respond)
PHP Regular Expressions Syntax,
Functions & Cheat Sheets
(http://www.noupe.com/php/php-regular-
expressions.html) said: August 24, 2009 at
3:12 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-41337)
[...] 15 PHP Regular Expression for Web
Developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=41410#respond)
Wordpress Blog Services - Getting Started
with PHP Regular Expressions
(http://www.wpconfig.com/2009/08/24/getting-
started-with-php-regular-expressions/) said:
August 24, 2009 at 3:42 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-41410)
[...] 15 PHP Regular Expression for Web
Developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=41448#respond)
Getting Started with PHP Regular
Expressions | SeanBurdick
(http://www.seanburdick.com/200908/getting-
started-with-php-regular-expressions/) said:
August 24, 2009 at 10:04 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-41448)
[...] 15 PHP Regular Expression for Web
Developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=41452#respond)
Enver Salih Yaylacı » düzenli ifadeler
ve php ( regular expression in php ) -
makale derlermesi
(http://www.enversalih.com/wp/php-
mysql/duzenli-ifadeler-ve-php-regular-
expression-in-php-makale-derlermesi/)
said: August 24, 2009 at 10:46 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-41452)
[...] http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-developers
[...]
Jamie Bicknell said: August 25, 2009 at
10:24 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-41524)
#9 Matching hexadecimal color values the
regex doesnt work for all hexadecimal codes,
for examble numeric ones, and alpha
numeric. I suggest that this regex is used
instead:
To Validate a Hexadecimal Colour, with or
without the #
/ (#)?([0-9a-f]{1,2}){3}$/i
To Validate a Hexadecimal Colour, WITH #
/ #([0-9a-f]{1,2}){3}$/i
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=41524#respond)
You could use this to replace colours in a
string via:
$str = ‘#6699CC and #00ff44 and #ff0000′;
echo preg_replace(‘|#([0-9a-f]{1,2})
{3}|i’,'$0′,$str);
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=41597#respond)
Getting Started with PHP Regular
Expressions - Programming Blog
(http://www.neurosoftware.ro/programming-
blog/blogposter/web-resources/getting-
started-with-php-regular-expressions/) said:
August 26, 2009 at 1:20 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-41597)
[...] 15 PHP Regular Expression for Web
Developers [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=41836#respond)
Rachel (http://progesteronedeficiency.net)
said: August 26, 2009 at 5:39 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-41836)
That sure cleared some things up! Thanks a
lot, keep writing stuff like that. I m in here on
a regular basis just to learn. =)
15 PHP regular expressions for web
developers | TopRoundups
(http://toproundups.com/2009/07/27/15-
php-regular-expressions-for-web-
developers/) said: August 30, 2009 at 1:47
am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-42404)
[...] 15 PHP regular expressions for web
developers Submitted by Editorial Team [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=42404#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=42870#respond)
John
(http://becometaller.net/becometaller) said:
September 1, 2009 at 7:12 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-42870)
Great info, sure cleared some things up.
Guess I still got a couple of things to learn,
though. Thank you.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=43084#respond)
Lo.J said: September 3, 2009 at 12:57 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-43084)
Matching a XML/HTML tag: example does not
work, same problem on the source website.
copy/paste is ok. copy/paste+test is better.
But i have no anger. Let the light of love shine
upon your soul.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=43256#respond)
David Winstead (http://davidwinstead.com)
said: September 4, 2009 at 10:44 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-43256)
I use Regular Expressions everyday, could
not function without them. Great list by the
way!
17 PHP Practices That Should Be Banished
Forever « Technology Strategy Center
(http://technologystrategy.wordpress.com/2009/09/06/17-
php-practices-that-should-be-banished-
forever/) said: September 6, 2009 at 4:49
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=43398#respond)
am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-43398)
[...] tool which when used correctly, can
accomplish some pretty amazing things as
demonstrated in this excellent article.
However, they are far slower than the native
PHP string manipulation functions and tend
to make [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=44114#respond)
RWH (http://www.reliablewebhosting.us/)
said: September 12, 2009 at 5:28 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-44114)
Highly informative and useful post. This would
be a great reference guide to any web
developer out there from beginner to advanced
level. All I can say is, “Bravo”!
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=45208#respond)
Craig (http://www.waspnestremoval.co.uk)
said: September 20, 2009 at 2:04 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-45208)
This is useful stuff, will be bookmarking this
page!
Has anyone got a good introduction to php
page to look at?
Alex Volkov » Ðрхив »
Полезные регулÑÑ
€ÐºÐ¸. (http://alexvolkov.ru/poleznye-
regulyarki.html) said: September 25, 2009
at 2:03 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-45996)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=45996#respond)
[...] ИÑточник [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=46911#respond)
Mark (http://www.faceblogger.com/) said:
October 1, 2009 at 10:17 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-46911)
How can I do this?
Consider the following sms header
To: #
Modem: GSM1
Sent: 09-09-21 13:25:39
Message_id: 69
IMSI: 413022600405030
I need to get the Message id (number only)
Please help me to do it…
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=47659#respond)
sunnybear (http://www.web-optimizer.us/)
said: October 7, 2009 at 12:47 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-47659)
TItle regular expression is incorrect. TITILE
tag can have a number of attribues (i.e. lang),
so the right one will be the following:
$titre = eregi(“]*>(.*)”,$page,$regs);
Hans Zena said: October 29, 2009 at 5:27
am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-51060)
Wonderful list. Here is a very useful regex
tester for PHP.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=51060#respond)
www.pagecolumn.com/tool/pregtest.htm
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=51222#respond)
web developer
(http://codeguru.services.officelive.com/default.aspx)
said: October 29, 2009 at 10:59 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-51222)
I am having a weird problem with Regex to
validate domain names. I am using the
following expression “/ (http|https|ftp):\/\/([A-
Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?
(\d+)?\/?/i”
It works fine the first time the user same this
regular expression. But if the user saves the
same expression the next time, it stops
working. Also if I check the expression at this
point all the back slashes \ are missing from
the expression. What could be the problem.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=53276#respond)
Chris ward (http://www.carwaxes.co.uk)
said: November 11, 2009 at 1:05 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-53276)
Great blog post, exactly what i was looking
for and has saved me time figuring it out for
myself. Reg-ex is a specialist subject all on
its own
neel (http://snilesh.com) said: December 7,
2009 at 9:41 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-57433)
Excellent Article. I learned alot from this
article. I was always scared learning regular
expressions. but from your article i learned it
easily.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=57433#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=64168#respond)
Regular Expressions : Enterprise Research
Systems (http://ers.asu.edu/blog/?p=152)
said: January 12, 2010 at 3:58 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-64168)
[...] the limit but that’s also much more than
most of us need. I found this simple list of
15 common regular expressions covers most
of what many of us need. Here’s a great
cheat sheet to jog your memory once [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=65995#respond)
Tim said: January 25, 2010 at 9:11 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-65995)
This looked like such a great reference. Until I
tried copy and paste of one of the code
snippets. Then read the comments and
realized that some might not work. Not the
author’s fault, I’m sure, but it’s disappointing
nonetheless it’s been this long and he hasn’t
been back to rectify the fault. Another waste
of time.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70090#respond)
Pascal said: March 1, 2010 at 10:09 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70090)
Remove repeated punctuation is kinda stupid,
if you don’t escape the dot. (;
Ryan Mauger (http://www.rmauger.co.uk)
said: March 1, 2010 at 12:33 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70111)
I looked forward to reading this post when I
saw smashing-mag tweet about it… but I
must say I’m quite disappointed to see
regular expressions being used for some of
the tasks you have outlined.
The opening was quite good, but I have to
note, you missed than \w can replace “a-zA-
Z0-9_” and \d for 0-9, but those are quite
forgivable oversights.
Immediatly I’m confronted with a regex to
validate a domain name, which fails! it does
nothing to check the validity of the top level
domain (this would be an enormous regex,
which is why regex’s are not recommended
for this task), not to mention not being able to
copy with new punycode domains.
The next problem is, using regular
expressions for parsing a html document?
this is madness. regular expressions are
hugely inefficient for this task. If you wish to
grab a title, all the a tags, all the img tags, or
query a HTML dom for an attribute, use a
proper DOM parser. There is a fantastic one
built into PHP, called DOM.
And the last gripe, using a regex to parse an
apache log. This is just silly. an apache log
could be quite huge. You should write a
proper parser for the task, and parse the log
through a stream, and not be using a regex
against the whole log. Using a regex for this
task is going to eat a huge amount of
memory, and be slow as hell!
I’m sorry that these complaints are strongly
worded, but you have a huge audience here,
you should be doing a much better job than
this. People will not advance as coders
without pointing in the RIGHT direction, this is
taking them off at a tangent they will need to
unlearn in the future!
There are some valid usecases for regular
expressions in this post, removing repeated
words, doubled punctuation, and even
swapping in smileys, but Its sad to see the
rest on a post in a site like this. shame on
you!
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70111#respond)
Reply (/blog/15-php-regular-
expressions-for-web-
developers?
replytocom=70225#respond)
Jean-Baptiste Jung
(http://www.wprecipes.com)
said: March 2, 2010 at 8:37 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-
web-developers#comment-
70225)
Thanks for the input Ryan. I agree
that some items of this posts are
“quick and dirty”, such as the
“Parse apache log” regexp. Thanks
for pointing it out, comments like
yours are useful in order to
enhance the site quality.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70168#respond)
VaxCave » Daily links for March 01, 2010
(http://vaxcave.com/2010/03/01/daily-links-
for-march-01-2010/) said: March 1, 2010 at
10:03 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70168)
[...] links for March 01, 2010 Filed under:
General — by Automaton on 3/1/2010 @
9:55 pm 15 PHP regular expressions for web
developers Essentially an intro to regex.
(tags: webdesign programming regex)
Comments [...]
jack fuchs said: March 2, 2010 at 7:59 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70221)
The above comment from Ryan Mauger is a
good starting point regarding the contents of
this post.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70221#respond)
Stupid regular expressions, miserable code
(PHP). Such a bad post.
You copied the contents for this post from
across the web, without having an idea on
what bullshit you are spreading, did you?
“15+ extremely useful regular expressions
that any web developer should have in his
toolkit”
Sorry, but it’s embarrassing.
Reply (/blog/15-php-regular-
expressions-for-web-
developers?
replytocom=70224#respond)
Jean-Baptiste Jung
(http://www.wprecipes.com)
said: March 2, 2010 at 8:32 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-
web-developers#comment-
70224)
Ryan Mauger explained why he
didn’t liked the post, which is a
good thing. Too bad you didn’t. If
you have better regexp, I’ll be
happy to publish them, feel free to
share.
Ryan Mauger
(http://www.rmauger.co.uk) said:
March 2, 2010 at 9:04 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-
web-developers#comment-
70228)
Im sorry, but I did not mean to
start any sort of flaming here.
merely to voice my
disappointment. There is no need
to be insulting in the way you are
here.
Jean-Baptiste generally does a
good job on this site, and hopefully
my comments will push him to
Reply (/blog/15-php-regular-
expressions-for-web-
developers?
replytocom=70228#respond)
improve his quality. Comments like
yours will only strive to anger and
upset, and achieve nothing.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70254#respond)
links for 2010-03-02 | Widmann Martin
Blog
(http://www.widmann.org/2010/03/02/links-
for-2010-03-02/) said: March 2, 2010 at
12:05 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70254)
[...] 15 PHP regular expressions for web
developers (tags: examples php regular
expressions regexp) [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70304#respond)
Bookmarks vom 02.03.2010
(http://www.kopfpit.de/wordpress/2010/03/03/bookmarks-
vom-02-03-2010/) said: March 2, 2010 at
7:16 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70304)
[...] 15 PHP regular expressions for web
developers – [...]
Espressioni regolari PHP: un’ottima
collezione! | Edit - Il blog di HTML.it
(http://blog.html.it/03/03/2010/espressioni-
regolari-php-unottima-collezione/) said:
March 3, 2010 at 1:59 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70356)
[...] buon Jean Baptiste Jung ha riunito in un
articolo ben 15 utilissimi usi delle espressioni
regolari, tutte facenti uso del [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70356#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70656#respond)
uberVU - social comments
(http://www.ubervu.com/conversations/www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers) said: March 4, 2010 at 11:30
pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70656)
Social comments and analytics for this
post…
This post was mentioned on Twitter by
catswhocode: 15 PHP regular expressions for
web developers http://tr.im/ueKB (Please
RT!!)…
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=70888#respond)
Charles Stover (http://charlesstover.com)
said: March 6, 2010 at 2:09 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-70888)
[a-zA-Z0-9_]{1,}$
Any word of at least one letter, number or _
What is this about? Whatever happened to
\w+$
Reply (/blog/15-php-regular-
expressions-for-web-
developers?
replytocom=99336#respond)
jack fuchs said: August 20, 2010
at 3:44 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-
web-developers#comment-
99336)
) 1+
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=71583#respond)
» ce que j’ai vue depuis mon retour !
oxynel, blog communication - Agence de
communication Montpellier
(http://blog.oxynel.com/oxynel/ce-que-jai-
vue-depuis-mon-retour_2010_03) said:
March 10, 2010 at 11:02 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-71583)
[...] 15 expressions régulières pour les
développeurs PHP [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=74181#respond)
10+ regular expressions for efficient web
development
(http://www.catswhocode.com/blog/10-
regular-expressions-for-efficient-web-
development) said: March 30, 2010 at
10:11 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-74181)
[...] you’re interested in regular expressions,
make sure you have read our “15 PHP regular
expression for developers” post. Share this on
del.icio.us Digg this! Stumble upon
something good? Share it on [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=75665#respond)
Le petit journal du web #3 — Spécial
CSS3 (et miscellanées)
(http://css4design.com/le-petit-journal-du-
web-3-special-css3-et-miscellanees) said:
April 9, 2010 at 11:49 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-75665)
[...] 15 PHP Regular Expressions — 15 trucs
et astuces PHP pour développeurs web. [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=90510#respond)
Justin said: June 30, 2010 at 1:33 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-90510)
To all of you who get : Warning: preg_match()
[function.preg-match]: Unknown modifier ‘/’
Simply replace the first ” / ” and last ” / ”
(before the “i”) of the pattern (in the
preg_match) with the symbol ” # “.
So the first line of the condition should give
you this:
if (preg_match(‘# (http|https|ftp)://([A-Z0-9][A-
Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?#i’,
$url)) {
The regex in itself is not too efficient, but if
you think it will be enough for your website,
just copy/paste my modifications.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=94061#respond)
Learning Regular Expressions |
developingElegance();
(http://developingelegance.com/php/learning-
regular-expressions/) said: July 20, 2010 at
10:11 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-94061)
[...] PCRE page Learning to Use Regular
Expressions by Example (might be a bit
outdated, we’ll see) 15 PHP Regular
Expressions For Web Developers – Cats
Who [...]
PHP Helps | The Michaeldon Roareth! -
Mad rantings of an undiscovered dinosaur
(http://mike.mikensara.net/?p=39) said:
August 2, 2010 at 11:43 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-96321)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=96321#respond)
[...] http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-developers –
I’ll be the first to admit that regular
expressions aren’t my strong suit. This has
some very useful ones for those of us that
aren’t as regex inclined. [...]
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=98603#respond)
15 PHP Regular Expressions For Web
Developers | PHPprogramátor.info
(http://www.phpprogramator.info/2010/08/15-
php-regular-expressions-for-web-
developers/) said: August 16, 2010 at 4:18
am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-98603)
[...] » Čítať ďalej originálny článok Komentáre
(0) k článku “15 PHP Regular Expressions
For Web Developers” [...]
Ipsita said: August 19, 2010 at 9:12 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-99216)
Can any one please help me to solve my
problem?
I have more than one label tags () with same
class name but different id in my template
file.
Ex:
I am here
Asking for help
like this.
The data given is only the class name.(Here
in the example class name is
“user_home_tpl_html”)
The pattern of the value of the id attribute is id
with an integer.(such as id1,id2….id200…
id333)
Now I need the regular expression to get the
content of the label tag
(Here in the example
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=99216#respond)
1: I am here
2: Asking for help
)
I’ll be obliged if anybody will be interested to
help me
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=104056#respond)
Floris said: September 14, 2010 at 12:25
am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-104056)
for the record: document.images fits best if
you need to have all images. It is way faster
than using regexes.
the example on this page only fits if you want
to retrieve all images from a different page
than the one that is currently loaded
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=104239#respond)
Sebastian (http://www.endless-loving.com)
said: September 14, 2010 at 7:40 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-104239)
That is really useful! Many thanks!
flimh (http://none) said: October 11, 2010
at 12:14 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-117726)
Practice REGEX by running this PHP snippet
CUT-PASTE-RUN
————————-
<?php
echo '’;
echo ‘Enter sample straing: ‘;
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=117726#respond)
echo ‘Enter match pattern: ‘;
echo ”;
echo ”;
if($_POST)
{
$string = $_POST['sampleString'];
$cclass = $_POST['matchPattern'];
}
if(preg_match(“/$cclass/”,$string))
{
echo ‘Old string: ‘.$string.”;
echo ‘Match pattern: ‘.$cclass.”;
$string = preg_replace(“/$cclass/”,’*',$string);
echo ‘New string: ‘.$string.”;
}
else
echo ‘No match’;
?>
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=124362#respond)
Brett Widmann
(http://www.brettwidmann.com) said:
October 21, 2010 at 7:09 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-124362)
So helpful! I’ve tried a few and they work
really well.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=132175#respond)
Derek Ashland
(http://www.orthosherbrooke.com) said:
November 3, 2010 at 11:57 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-132175)
I got to show off my knowledge in class
because of your post. Now people think I’m a
PHP God!
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=226635#respond)
alex said: January 31, 2011 at 6:58 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-226635)
Thanks for the post. Very useful.
A little fix, maybe: ‘/ (http|https|ftp)://([A-Z0-9]
[A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?
(d+)?/?/i’
“://” => “:\/\/” (requires escaping
in the end )?/?/i
should be: ?/i
so: ‘/ (http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:.
[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/i’
above produces no error.
Thanks for the useful post, once again!
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=380901#respond)
Rick (http://ifandbut.com) said: June 4,
2011 at 10:52 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-380901)
Hello,
Very nice post, it helped us to give example
for starter . I have used your post as example
for those who want to get first glimpse of
regex in PHP.
nikk (http://muzikki.com) said: July 27,
2011 at 5:33 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-444779)
Hi,
I got some code that i would like to locate
and replace certain text that determine width
and height of a youtube video
programmatically. I got this youtube code:
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=444779#respond)
I would like to locate all instances of width
and height and change these to lets say
(height=200 and width=”400″) so the code
would read like this:
Any assistance would be greatly appreciated.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=570297#respond)
Syler (http://techstates.com) said:
November 22, 2011 at 3:42 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-570297)
Use this for domain name with port path and
query to verify it.
$hostRegex = “([a-z\d][-a-z\d]*[a-z\d]\.)*[a-z][-
a-z\d]*[a-z]“;
$portRegex = “(:\d{1,})?”;
$pathRegex = “(\/[ ?#\"\s]+)?”;
$queryRegex = “(\?[ #\"\s]+)?”;
$urlRegex = “/(?:(?<= )|(?<=\s))((ht|f)tps?:\/\/"
. $hostRegex . $portRegex . $pathRegex .
$queryRegex . ")/";
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=689899#respond)
Tom (http://www.onlinesurveys.co.uk/)
said: February 7, 2012 at 10:50 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-689899)
Good tips. I’ve still got a long way to go, but
I’ve finally decided PHP is the language for
me. Fingers crossed within a few months I’ll
understand what all of these mean better!
Tom
Recep Kırmızı (http://www.rkirmizi.org)
said: February 16, 2012 at 3:19 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=699654#respond)
developers#comment-699654)
(gif|jpg) Matches either “gif” or “jpeg”
this does not match jpeg, it will match gif or
jpg. just a little typo i guess.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=712523#respond)
Dana (http://www.bigfatdiary.com) said:
February 29, 2012 at 5:08 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-712523)
what if I want to store (save) all the title and
links from the first page in a file
(test_google.csv) when I search on google the
word “test” ? How should I write the condition
and the preg_match_all.
this is how I extract and post the data:
$curl = new Curl (true, “cookieFile.txt”);
$googlePage = $curl -> get(“http://…………”);
print $googlePage; die ();
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=733721#respond)
Bridget said: March 20, 2012 at 2:39 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-733721)
Great article!! To the point! Every other article
I find to try and understand regex STILL
makes it sooo hard!
Reply (/blog/15-php-regular-expressions-
for-web-developers?
suresh (http://www.150dpi.com) said: June
15, 2012 at 1:46 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-820428)
this is cool. thanks dude.
replytocom=820428#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=847254#respond)
syndrael (http://www.pure-tentation.fr)
said: July 16, 2012 at 9:14 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-847254)
Great article but for HTML or XML, i really
prefer DomDocument.. Easier and more
dedicated.
S.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=948719#respond)
Mohammad Faisal said: September 29,
2012 at 2:19 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-948719)
I want to check whether the string contains
only numbers or not. Like verifying the mobile
number.
what will be the code snippet?
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=988588#respond)
Bhavana (http://www.springedge.com)
said: October 26, 2012 at 9:26 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-988588)
How to extract ” 4 Valid 10-digit number(s)
are recorded!! ” form string “ pls help
Martin (http://www.mgscreativa.com) said:
January 23, 2013 at 10:18 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-1131640)
Great function “Matching an XHTML/XML tag
with a certain attribute value”
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=1131640#respond)
In my case i had to retouch the return by this
return $matches[3][0];
Adding [0] because I wanted the string.
I used like this and worked!
$contents =
file_get_contents(‘http://site.com/page.php’);
echo get_tag(‘id’, ‘spamID1, $contents,
‘span’);
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=1136934#respond)
chris (http://nah) said: January 27, 2013 at
4:19 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-1136934)
I wanted to use preg_split() and test for either
a / or a – or a .
$splitDate= preg_split(‘[/-.]‘, $mydate);
I will not work, it will only work with ONE
arguement
$splitDate= preg_split(‘[/]‘, $mydate);
$splitDate= preg_split(‘[-]‘, $mydate);
$splitDate= preg_split(‘[.]‘, $mydate);
why is this? any answers gratefuilly recieved.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
Virneto (http://www.phronzs.com) said:
February 21, 2013 at 11:01 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-1161089)
Boy, what a wonderfull post this was for me!!!
Thanks a lot for the explanation and for the
code examples!!!
and best regards to you!
Virneto
replytocom=1161089#respond)
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=1571862#respond)
Kasun said: October 9, 2013 at 8:03 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-1571862)
How am I catch html tag like follow
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=1720880#respond)
Junaid Ahmed
(http://www.displacednames.com/) said:
November 3, 2013 at 3:50 am
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-1720880)
nice and well done.
great collection.
Reply (/blog/15-php-regular-expressions-
for-web-developers?
replytocom=2681278#respond)
Nicky said: December 22, 2013 at 4:16 pm
(http://www.catswhocode.com/blog/15-
php-regular-expressions-for-web-
developers#comment-2681278)
all..
great post! thank you!
i need following solution…
using
$name2 = preg_replace(“[[0-9]{4}]”, “”, $name
);
this works fine when i have:
nicky2547
but when i have nickyA12213
how to get that work that ONLY last 4 digits
will be replaced… to get nickyA1 as result
and not nickyA3.
thank you in advance and wish you a marry
christmas!
Post Comment
Leave a Reply
Your email address will not be published. Required fields are
marked *
Name *
Email *
Website
Comment
Please respect the following rules: No advertising, no
spam, no keyword in name field. Thank you!
(http://feeds2.feedburner.com/Catswhocode)
© 2008 - 2014 CatsWhoCode.com (http://www.catswhocode.com)