You are on page 1of 6



Using Regular Expressions with PHP








C ontact


Using Regular Expressions with PHP

or browse popular tags

PHP Tutorial
What is PHP? What can php do for you? How to install and cofigure PHP 5 on Windows box Your first PHP Script Variables C onstants Arrays Sorting Arrays Multidimensional Arrays C onditional Statements Looping Statements Functions Form Processing Using C ookies in PHP Dynamic Image Generation File Upload Sending Email (Text/HTML/Attachments) Working with Directories How to connect to MySQL database using PHP How to connect to MSSQL database DSN and DSN-less connections Blocking access to the login page after three unsuccessful login attempts How to Encrypt Passwords in the Database How to C reate C APTC HA Protection using PHP and AJAX Passing JavaScript variables to PHP How to Get the C urrent Page URL Export Database Schema as XML C reate Thumbnail Images using PHP Using Regular Expressions with PHP Reading the "clean" text from DOC X and ODT with PHP PHP: Reading the "clean" text from RTF Reading the "clean" text from PDF with PHP Debugging PHP with Xdebug C reating Word, Excel and C SV files with PHP Record locking in Web applications jQuery File Upload How to create a dump of MySQL database in one click Bike - drop in phpMyAdmin replacement

Regular expressions are a powerful tool for examining and modifying text. Regular expressions themselves, with a general pattern notation almost like a mini programming language, allow you to describe and parse text. They enable you to search for patterns within a string, extracting matches flexibly and precisely. However, you should note that because regular expressions are more powerful, they are also slower than the more basic string functions. You should only use regular expressions if you have a particular need. This tutorial gives a brief overview of basic regular expression syntax and then considers the functions that PHP provides for working with regular expressions. The Basics Matching Patterns Replacing Patterns Array Processing PHP supports two different types of regular expressions: POSIX-extended and Perl-Compatible Regular Expressions (PCRE). The PCRE functions are more powerful than the POSIX ones, and faster too, so we will concentrate on them.

The Basics
In a regular expression, most characters match only themselves. For instance, if you search for the regular expression "foo" in the string "John plays football," you get a match because "foo" occurs in that string. Some characters have special meanings in regular expressions. For instance, a dollar sign ($) is used to match strings that end with the given pattern. Similarly, a caret (^) character at the beginning of a regular expression indicates that it must match the beginning of the string. The characters that match themselves are called literals. The characters that have special meanings are called metacharacters. The dot (.) metacharacter matches any single character except newline (\). So, the pattern h.t matches hat, hothit, hut, h7t, etc. The vertical pipe (|) metacharacter is used for alternatives in a regular expression. It behaves much like a logical OR operator and you should use it if you want to construct a pattern that matches more than one set of characters. For instance, the pattern Utah|Idaho|Nevada matches strings that contain "Utah" or "Idaho" or "Nevada". Parentheses give us a way to group sequences. For example, (Nant|b)ucket matches "Nantucket" or "bucket". Using parentheses to group together characters for alternation is called grouping. If you want to match a literal metacharacter in a pattern, you have to escape it with a backslash. To specify a set of acceptable characters in your pattern, you can either build a character class yourself or use a predefined one. A character class lets you represent a bunch of characters as a single item in a regular expression. You can build your own character class by enclosing the acceptable characters in square brackets. A character class matches any one of the characters in the class. For example a character class [abc] matches a, b or c. To define a range of characters, just put the first and last characters in, separated by hyphen. For example, to match all alphanumeric characters: [a-zA-Z0-9]. You can also create a negated character class, which matches any character that is not in the class. To create a negated character class, begin the character class with ^: [^0-9]. The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched. + means "Match one or more of the preceding expression", * means "Match zero or more of the preceding expression", and ? means "Match zero or one of the preceding expression". Curly braces {} can be used differently. With a single integer, {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,} means "match n or more occurrences of the preceding expression", and with two comma-separated integers {n,m} means "match the previous character if it occurs at least n times, but no more than m times". Now, have a look at the examples:
Regular Expression foo ^foo foo$ ^foo$ [abc] [a-z] [^A-Z] (gif|jpg) [a-z]+ [0-9\.\-] ^[a-zA-Z0-9_]{1,}$ ([wx])([yz]) [^A-Za-z0-9] ([A-Z]{3}|[0-9]{4}) Will match... The string "foo" "foo" at the start of a string "foo" at the end of a string "foo" when it is alone on a string a, b, or c Any lowercase letter Any character that is not a uppercase letter Matches either "gif" or "jpeg" One or more lowercase letters Аny number, dot, or minus sign Any word of at least one letter, number or _ wy, wz, xy, or xz Any symbol (not a number or a letter) Matches three letters or four numbers

Perl-Compatible Regular Expressions emulate the Perl syntax for patterns, which means that each pattern must be enclosed in a pair of delimiters. Usually, the slash (/) character is used. For instance, /pattern/. The PCRE functions can be divided in several classes: matching, replacing, splitting and filtering.


] [ A z 0 9 _ ] + ) * [ . Back to top Replacing Patterns In the above examples.php 2/6 . p r e g _ r e p l a c e (p a t t e r n . This checks if there are at least 8 characters in the string. one upper case letter and one digit: < ? p h p $ p a s s w o r d=" F y f j k 3 4 s d f j f s j q 7 " . Between are groupings in parentheses. } ? > The letters "ll" exist in "Hello". The ". * ( ? = . we will consider regular expression that validates an email address: < ? p h p $ e m a i l=f i r s t n a m e . e c h o$ m a t c h e s [ 0 ] .$ m a t c h e s ) ){ e c h o" M a t c hw a sf o u n d< b r/ > " . but looking for them with following characters: < ? p h p i f( p r e g _ m a t c h ( " / l l . An alternative function. { 8 . " . * [ a z ] ) ( ? = ." T h eH i s t o r yo fH a l l o w e e n " . " . leaving the search string untouched. The most popular use of regular expressions is validation. }e l s e{ e c h o" E m a i la d d r e s si s< u > n o t < / u >v a l i d . ] [ A z ] { 2 .30. ] [ A z 0 9 _ ] + ) * [ @ ] [ A z 0 9 _ ] + ( [ . a regular expression string. the password must be at least 8 characters and must contain at least one lower case letter. preg_match_all().s u b j e c t[ . " . the string to modify.$ e m a i l ) ){ e c h o" E m a i la d d r e s si sv a l i d . that one digit can appear anywhere in the string.e. preg_match() takes two basic and three optional parameters. in order. Finally. Subscription Sign up for the free email newsletter for new tips. Let's try to investigate this regular expression yourself. and * metacharacter means "zero or more". the . The regular expression in the next example is looking for the letters "ell".*{8. i f( p r e g _ m a t c h ( $ r e g e x p . and returns the number of times it matched.l i m i t] ) webcheatsheet. then any digit can happen".m a t c h e s[ .10. an array variable which stores matches. l a s t n a m e @ a a a . a flag argument and an offset parameter that can be used to specify the alternate place from which to start the search: p r e g _ m a t c h(p a t t e r n . a regular expression. }e l s e{ e c h o" Y o u rp a s s w o r di sw e a k . and the last optional argument which specifies how many matches will be replaced. Let's search the string "Hello World!" for the letters "ll": < ? p h p i f( p r e g _ m a t c h ( " / e l l / " .com/php/regular_expressions. it's saying that it must appear but we're not worried about the order. the text with which to replace a found pattern.f l a g s[ . These parameters are. e c h o$ m a t c h e s [ 0 ] . the preg_match() function matches only the first pattern it finds in a string. } ) ( ? = . in order. As mentioned above.$ m a t c h e s ) ){ e c h o" M a t c hw a sf o u n d< b r/ > " . This means it is very quick to check whether a pattern exists in a string. So this checks if there is at least one number in the string.$ p a s s w o r d ) ){ e c h o" Y o u rp a s s w o r d si ss t r o n g . i f( p r e g _ m a t c h ( " / ^ . The next groupings (?=. splitting and filtering.}).o f f s e t ] ] ] ) Subscribe The preg_match() function returns 1 if a match is found and 0 otherwise.r e p l a c e m e n t . The next grouping (?=. matches a pattern against a string as many times as the pattern allows. a source string. so preg_match() returns 1 and the first element of the $matches variable is filled with the string that matched the pattern.2013 Back to top Using Regular Expressions with PHP The PCRE functions can be divided in several classes: matching. } ? > The ^ and $ are looking for something at the start and the end of the string.*[a-z]) and (?=.* [0-9]) means "any alphanumeric character can happen zero or more times. preg_replace() takes three basic parameters and an additional one. Enter your email address below. The "?=" combination means "the next text must be like this".*[A-Z]) are looking for the lower case and upper case letter accordingly anywhere in the string. replacing.(dot) metacharacter means any alphanumeric character.s u b j e c t[ . Privacy Policy Matching Patterns The preg_match() function performs Perl-style pattern matching on a string. The preg_replace() function looks for substrings that match a pattern and then replaces them with new text. and then click the button. * [ A Z ] ) . } ? > Now let's consider more complicated example. 4 } $ / " . The first grouping is (?=. i. In this example. The example below checks if the password is "strong". * / " . b b b . instead of specifying the order that things should appear. we have searched for patterns in a string. tutorials and more. These parameters are. * $ / " . But since the string isn't captured. $ r e g e x p=" / ^ [ ^ 0 9 ] [ A z 0 9 _ ] + ( [ . * \ d ) ( ? = . This construct doesn't capture the text." H e l l oW o r l d ! " . " .*" combination is used at both the start and the end. For the speed reasons. } ? > This regular expression checks for the number at the beginning and also checks for multiple periods in the user name and domain name in the email address. c o m .

" 2 0 0 7 0 1 2 5 " ) . s t r t o u p p e r ( " $ 2 " ) ' . and so on are set to the individual matches for each subpattern. You can refer to the text matched by subpattern with a dollar sign ($) and the number of the subpattern. you should use parentheses to wrap any elements of your regular expression that you might want to | stumbleupon It has been dugg: http://digg. France. testing all elements against the supplied pattern. it will take strtoupper(name) and replace it with the result of the strtoupper() function. $ o u t p u t=p r e g _ g r e p ( ' / ^ [ a m ] / i ' . we should pass arrays of patterns and replacements. if you are using subpatterns." $ 2 / $ 3 / $ 1 " . To use this feature. Since we have appended an "e" to the end of the regular expression. " / ( \ d { 4 } ) ( \ d { 2 } ) ( \ d { 2 } ) \ s ( \ d { 2 } : \ d { 2 } : \ d { 2 } ) / " ) . $ can say to PHP that the match text should be executed as PHP code once the replacement has taken place. # Posted by Joetke | 3 Aug 2007 23:15:00 webcheatsheet.$ r e p l a c e . ? > Another useful PHP function is the preg_grep() function which returns those elements of an array that match a given pattern.$ s t r i n g ) . If a match is found. ' B i l l ' ) ." p h p . p r i n t _ r ($ o u t p u t) . In the following example we search for the copyright phrase and replace the year with the current." $ 12 0 0 7 " . < ? p h p e c h op r e g _ r e p l a c e ( " / ( [ C c ] o p y r i g h t )2 0 0 ( 3 | 4 | 5 | 6 ) / " . p r i n t _ r ($ k e y w o r d s) . To perform multiple substitutions on the same string or array of strings with one call to preg_replace(). the matching element is returned as part of the array containing all matches. The basic idea is the same as preg_match_all() except that. Back references make it possible for you to use part of a matched pattern in the replacement string. The following example searches through an array and all the names starting with letters A-J: < ? p h p $ n a m e s=a r r a y ( ' A n d r e w ' . In the following example we will change the date format from "yyyy-mm-dd" to "mm/dd/yyy": < ? p h p e c h op r e g _ r e p l a c e ( " / ( \ d + ) ( \ d + ) ( \ d + ) / " . The following example uses a regular expression to split the string by any number of commas or space characters: < ? p h p $ k e y w o r d s=p r e g _ s p l i t ( " / [ \ s . e c h op r e g _ r e p l a c e ( $ s e a r c h . That is. For instance. $ s t r i n g=" P o s t e db yJ o h n|2 0 0 7 0 2 1 50 2 : 4 3 : 4 1 " . # Posted by Fábio | 15 Apr 2007 23:09:06 This article is not a light intro to regexp in PHP. ? > In the above example we use back references in the replacement nor an advanced one but your code lines turned out to be straight useful for me." C o p y r i g h t2 0 0 5 " ) . When it's necessary to split a string with a dynamic expression rather than a fixed one. Have a look at the example: < ? p h p $ s e a r c h=a r r a y(" / ( \ w { 6 } \ s \ ( w { 2 } ) \ s ( \ w + ) / e " . ? > In the above example we use the other interesting functionality .com/programming/Awesome_PHP_regular_expression_cheatsheet # Posted by Frank | 2 Apr 2007 08:42:17 The REGEX (gif|jpg) match with gif or jpg.php 3/6 . This function traverses the input array. it returns an array of pieces that didn't match the specified pattern. which is NAME. ? > We also can pass an array of strings as subject to make the substitution on all of them. this function comes to the rescue. ' P e t e r ' .r e g u l a re x p r e s s i o n s " ) . " $ 3 / $ 2 / $ 1$ 4 " ) . ' N a s t i n ' .icio.$ n a m e s ) .30. $ r e p l a c e=a r r a y( ' " $ 1" . ' J o h n ' .2013 Using Regular Expressions with PHP The function returns the changed string if a match was found or an unchanged copy of the original string otherwise. ] + / " . Back to top Array Processing PHP's preg_split() function enables you to break a string apart basing on something more complicated than a literal sequence of characters. $0 is set to the whole match.10. ? > Back to top Tags: Add To: dzone | digg | del. Thanks a lot from Strasbourg. not gif or jpeg. PHP will execute the replacement it makes. then $1. instead of returning matched pieces of the subject string.

. -1.2013 Using Regular Expressions with PHP # Posted by Joetke | 3 Aug 2007 23:15:00 very nice explanation . Please provide me php script for this. Shailendra # Posted by shailendra | 4 Oct 2007 03:25:23 It is really a good article to start with and I got helped from this article. $string).com/v/qgAYasUw6vk" type="application/x-shockwave-flash" wmode="transparent" width="425" height="366"></embed></object> <-Using regular repression but can't able to found I want to replace height=200 and width=300 from following string --> <object width="425" height="366"><param name="movie" value="http://www.10. # Posted by Archana | 10 Aug 2007 03:09:49 Very helpful note. So.?> Thanks in advance! # Posted by C hillyroll | 11 Jan 2008 23:15:58 webcheatsheet.. Ram Prasath. explained the regex concepts clearly...R # Posted by ram | 22 Nov 2007 04:01:44 hi how do i do this KarenFred123 to Karen Fred MichaelFrance488 to Michael France i did this $str = 'AasdasdasGasasd'.youtube.php 4/6 . So.strtoupper("$2")'.net. but it outputs Array ( [0] => asdasdas [1] => asasd ) thanks very much # Posted by shane | 23 Nov 2007 05:28:15 First of all. Stefan # Posted by Stefan | 3 Nov 2007 14:17:52 dear Shailendra. # Posted by Jeroen | 18 Nov 2007 18:28:52 really a good one.30. Best Regards. thanks a ton. "/(d{4})-(d{2})-(d{2})s(d{2}:d{2}:d{2})/")"></param> <param name="wmode" value="transparent"></param><embed src="http://www. I never understood it until now. PREG_SPLIT_NO_EMPTY). $string = "Posted by John | 2007-02-15 02:43:41". $str. Now. can you explain how does it work? I mean I got the grouping logic but where the strtoupper() function is getting applied? <?php $search = array ( "/(w{6}s(w{2})s(w+)/e". try using str_replace.. echo preg_replace($search. "$3/$2/$1 $4"). print_r($chars). Thanks.. manual on php. $replace = array ('"$1 ". this regex tut is awesome.. # Posted by Zia | 30 Oct 2007 01:52:46 Extreamly useful info and a great reference when dealing with regular expressions! # Posted by Justin | 1 Nov 2007 03:43:05 ([A-Z]{3}|[0-9]{4}) matches three UPERC ASE letters or 4 digits... Really simple explanation on a really complex topic.. $replace. # Posted by jijo | 16 Aug 2007 03:11:25 This is the BEST explanation of regular expression I have ever seen. # Posted by Yacahuma | 21 Aug 2007 08:47:53 Please healp me. the only part that I didn't understand is the following regex. Usefull exlanation using regexps. $chars = preg_split('/[A-Z]/'.

out. its kind of tricky stuff i belive.println(" ..out. listA. god bless you. good and great job. I added a few lines to make it stronger and account for users who might fat-finger in a space in the password or at begin or space at end. System.Storing Integer(" + i + ")")..println("+---------------------------------------------------------------------+")">Homes for sale</a>-Homes for sale # Posted by jonathan | 16 Mar 2009 10:52:47 Excellent System.php 5/6 . # Posted by ronair | 27 Mar 2008 02:58:06 This is a realy good explanation of regular expressions. So i'm posting two comments in same day. webcheatsheet. */ public void doLinkedListExample() { final int MAX = # Posted by C haim C haikin | 13 Apr 2008 02:00:32 Great tutorial.. i swear the god. C haim C haikin http://chaimchaikin. excited..out.10. no ads here how do you survive. Its really awsome.add("Jeff"). ========= jonathan ========= <a href="http://fsbo. its the best php topic. i < MAX. Very useful for tyro in Reg exp. Thanks. List listB = new LinkedList().out. |"). # Posted by Arun | 27 May 2008 13:44:28 Very nice article. System. System. I'm remembering the day when i've searched for the regx.Storing String(Jeff)"). System. <.println(" . System.OK! you'll c me here all the day.2013 Using Regular Expressions with PHP Great.out...out..out. # Posted by Sumitra Acharya | 17 Jan 2008 06:20:46 Really fantastic step-by-step explanation.add("Alex"). i++) { System.Storing String(Melody)"). for (int i = 0.println(" .add("Melody").fastrealestate.Storing String(Alex)").println("| C reate/Store objects in an LinkedList container.add(new Integer(i)). int counter = 0. I decided to stop learning php b4 I read this explanation. India..Storing String(Alex)"). listA.add("Alex").out. I have been using PHP for quite a while but I have never known how to use regular expressions. # Posted by Muthukumaran | 5 Feb 2008 22:17:43 this topic "must seen" thing . better than official php site.println(" .. listA. ADDED 2 favorite. Thank you so very much from Pune.println().. } System. listA.println(" . Great. # Posted by rania | 29 Feb 2008 10:14:13 very nice site # Posted by narendra ojha | 23 Mar 2008 00:45:51 Hey pretty straight and useful!!! Thanks a ton. List listA = new LinkedList(). Made regular expressions easy for me.. Very helpful site.println("+---------------------------------------------------------------------+"). very helpful # Posted by Yogesh Agrawal | 28 May 2008 08:04:03 Hello Guys! /** * Provides an example of how to work with the LinkedList container. and thank you very much.30.

} else { echo "Your password is weak.*d)(?" type="application/x-shockwave-flash" wmode="transparent" width="425" height="366"></embed></object>'.*$/".".})(?=.php 6/6 .WebC heatSheet. $patterns[0] = "/[Ww]idth="[0-9][0-9][0-9]"/". $replacements[0] = 'width="400"'. Thank you very much! # Posted by Joshua | 22 Jun 2009 14:24:19 Thank you for this article. # Posted by ssk | 8 Jun 2009 04:38:06 This is BY FAR the best tutorial/guide to pattern matching I have seen on the All Rights Reserved. //Find-Do not allow space in password and allow the spec chars !@#$%* //Results in true strong password and eliminates problems with spaces if ((!preg_match("/s/".*(?=.*[a-z])(?"> </param><param name="wmode" value="transparent"></param><embed src="$string).". $replacements[1] = 'height="400"'.{8.30. echo preg_replace($patterns. Took me a while to find a regex tutorial that covered grouping! # Posted by Josh L | 6 Oct 2009 17:27:36 C opyright © 2005-2012 www. webcheatsheet. $patterns[1] = "/[Hh]eight="[0-9][0-9][0-9]"/".$dPwd))&&(preg_match("/^.2013 <?php Using Regular Expressions with PHP //Trim space off front and end of Desired Password $dPwd = trim($dPwd).$replacements. } ?> # Posted by Rbh | 1 Apr 2009 06:22:53 Example for replace the width and height $string = '<object width="425" height="366"><param name="movie" value="http://www.*[!@#$\%*]).youtube.*[A-Z])(?=.10. $dPwd))){ echo "Your passwords is strong.