Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

30.10.

2013

Using Regular Expressions with PHP

Home

ASP

PHP

SQL

HTML

JavaScript

Search

C ontact

Search

Using Regular Expressions with PHP


Search

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.

webcheatsheet.com/php/regular_expressions.php

1/6

30.10.2013
Back to top

Using Regular Expressions with PHP


The PCRE functions can be divided in several classes: matching, replacing, splitting and filtering.

Subscription
Sign up for the free email newsletter for new tips, tutorials and more. Enter your email address below, and then click the button. Privacy Policy

Matching Patterns
The preg_match() function performs Perl-style pattern matching on a string. preg_match() takes two basic and three optional parameters. These parameters are, in order, a regular expression string, a source string, an array variable which stores matches, 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 ,s u b j e c t[ ,m a t c h e s[ ,f l a g s[ ,o f f s e t ] ] ] )
Subscribe

The preg_match() function returns 1 if a match is found and 0 otherwise. 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 / " ," H e l l oW o r l d ! " ,$ m a t c h e s ) ){ e c h o" M a t c hw a sf o u n d< b r/ > " ; e c h o$ m a t c h e s [ 0 ] ; } ? >

The letters "ll" exist in "Hello", so preg_match() returns 1 and the first element of the $matches variable is filled with the string that matched the pattern. The regular expression in the next example is looking for the letters "ell", but looking for them with following characters:
< ? p h p i f( p r e g _ m a t c h ( " / l l . * / " ," T h eH i s t o r yo fH a l l o w e e n " ,$ m a t c h e s ) ){ e c h o" M a t c hw a sf o u n d< b r/ > " ; e c h o$ m a t c h e s [ 0 ] ; } ? >

Now let's consider more complicated example. The most popular use of regular expressions is validation. The example below checks if the password is "strong", i.e. the password must be at least 8 characters and must contain at least one lower case letter, 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 " ; i f( p r e g _ m a t c h ( " / ^ . * ( ? = . { 8 , } ) ( ? = . * \ d ) ( ? = . * [ a z ] ) ( ? = . * [ A Z ] ) . * $ / " ,$ 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 . " ; }e l s e{ e c h o" Y o u rp a s s w o r di sw e a k . " ; } ? >

The ^ and $ are looking for something at the start and the end of the string. The ".*" combination is used at both the start and the end. As mentioned above, the .(dot) metacharacter means any alphanumeric character, and * metacharacter means "zero or more". Between are groupings in parentheses. The "?=" combination means "the next text must be like this". This construct doesn't capture the text. In this example, instead of specifying the order that things should appear, it's saying that it must appear but we're not worried about the order. The first grouping is (?=.*{8,}). This checks if there are at least 8 characters in the string. The next grouping (?=.* [0-9]) means "any alphanumeric character can happen zero or more times, then any digit can happen". So this checks if there is at least one number in the string. But since the string isn't captured, that one digit can appear anywhere in the string. The next groupings (?=.*[a-z]) and (?=.*[A-Z]) are looking for the lower case and upper case letter accordingly anywhere in the string. Finally, 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 . l a s t n a m e @ a a a . b b b . c o m ; $ r e g e x p=" / ^ [ ^ 0 9 ] [ A z 0 9 _ ] + ( [ . ] [ A z 0 9 _ ] + ) * [ @ ] [ A z 0 9 _ ] + ( [ . ] [ A z 0 9 _ ] + ) * [ . ] [ A z ] { 2 , 4 } $ / " ; i f( p r e g _ m a t c h ( $ r e g e x p ,$ 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 . " ; }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 . " ; } ? >

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. Let's try to investigate this regular expression yourself. For the speed reasons, the preg_match() function matches only the first pattern it finds in a string. This means it is very quick to check whether a pattern exists in a string. An alternative function, preg_match_all(), matches a pattern against a string as many times as the pattern allows, and returns the number of times it matched. Back to top

Replacing Patterns
In the above examples, we have searched for patterns in a string, leaving the search string untouched. The preg_replace() function looks for substrings that match a pattern and then replaces them with new text. preg_replace() takes three basic parameters and an additional one. These parameters are, in order, a regular expression, the text with which to replace a found pattern, the string to modify, and the last optional argument which specifies how many matches will be replaced.
p r e g _ r e p l a c e (p a t t e r n ,r e p l a c e m e n t ,s u b j e c t[ ,l i m i t] )

webcheatsheet.com/php/regular_expressions.php

2/6

30.10.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. In the following example we search for the copyright phrase and replace the year with the current.
< ? 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 ) / " ," $ 12 0 0 7 " ," C o p y r i g h t2 0 0 5 " ) ; ? >

In the above example we use back references in the replacement string. Back references make it possible for you to use part of a matched pattern in the replacement string. To use this feature, you should use parentheses to wrap any elements of your regular expression that you might want to use. You can refer to the text matched by subpattern with a dollar sign ($) and the number of the subpattern. For instance, if you are using subpatterns, $0 is set to the whole match, then $1, $2, and so on are set to the individual matches for each subpattern. 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 + ) / " ," $ 2 / $ 3 / $ 1 " ," 2 0 0 7 0 1 2 5 " ) ; ? >

We also can pass an array of strings as subject to make the substitution on all of them. To perform multiple substitutions on the same string or array of strings with one call to preg_replace(), we should pass arrays of patterns and replacements. 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 " , " / ( \ d { 4 } ) ( \ d { 2 } ) ( \ d { 2 } ) \ s ( \ d { 2 } : \ d { 2 } : \ d { 2 } ) / " ) ; $ r e p l a c e=a r r a y( ' " $ 1" . s t r t o u p p e r ( " $ 2 " ) ' , " $ 3 / $ 2 / $ 1$ 4 " ) ; $ 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 " ; e c h op r e g _ r e p l a c e ( $ s e a r c h ,$ r e p l a c e ,$ s t r i n g ) ; ? >

In the above example we use the other interesting functionality - you can say to PHP that the match text should be executed as PHP code once the replacement has taken place. Since we have appended an "e" to the end of the regular expression, PHP will execute the replacement it makes. That is, it will take strtoupper(name) and replace it with the result of the strtoupper() function, which is NAME. 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. When it's necessary to split a string with a dynamic expression rather than a fixed one, this function comes to the rescue. The basic idea is the same as preg_match_all() except that, instead of returning matched pieces of the subject string, it returns an array of pieces that didn't match the specified pattern. 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 , ] + / " ," p h p ,r e g u l a re x p r e s s i o n s " ) ; p r i n t _ r ($ k e y w o r d s) ; ? >

Another useful PHP function is the preg_grep() function which returns those elements of an array that match a given pattern. This function traverses the input array, testing all elements against the supplied pattern. If a match is found, the matching element is returned as part of the array containing all matches. 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 ' , ' J o h n ' , ' P e t e r ' , ' N a s t i n ' , ' B i l l ' ) ; $ o u t p u t=p r e g _ g r e p ( ' / ^ [ a m ] / i ' ,$ n a m e s ) ; p r i n t _ r ($ o u t p u t) ; ? >

Back to top

Tags: Add To: dzone | digg | del.icio.us | stumbleupon

It has been dugg: http://digg.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, not gif or jpeg. # Posted by Fbio | 15 Apr 2007 23:09:06 This article is not a light intro to regexp in PHP, nor an advanced one but your code lines turned out to be straight useful for me. Thanks a lot from Strasbourg, France. # Posted by Joetke | 3 Aug 2007 23:15:00

webcheatsheet.com/php/regular_expressions.php

3/6

30.10.2013

Using Regular Expressions with PHP


# Posted by Joetke | 3 Aug 2007 23:15:00 very nice explanation ..... # Posted by Archana | 10 Aug 2007 03:09:49 Very helpful note...... # Posted by jijo | 16 Aug 2007 03:11:25

This is the BEST explanation of regular expression I have ever seen. I never understood it until now. # Posted by Yacahuma | 21 Aug 2007 08:47:53 Please healp me. I want to replace height=200 and width=300 from following string --> <object width="425" height="366"><param name="movie" value="http://www.youtube.com/v/qgAYasUw6vk"></param> <param name="wmode" value="transparent"></param><embed src="http://www.youtube.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 soution. Please provide me php script for this. Best Regards, 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. # 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. Usefull exlanation using regexps. Thanks. Stefan # Posted by Stefan | 3 Nov 2007 14:17:52 dear Shailendra, try using str_replace, manual on php.net...

# Posted by Jeroen | 18 Nov 2007 18:28:52 really a good one, explained the regex concepts clearly.. Ram Prasath.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'; $chars = preg_split('/[A-Z]/', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars); but it outputs Array ( [0] => asdasdas [1] => asasd ) thanks very much # Posted by shane | 23 Nov 2007 05:28:15 First of all, this regex tut is awesome. Really simple explanation on a really complex topic. So, thanks a ton. Now, the only part that I didn't understand is the following regex. So, 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", "/(d{4})-(d{2})-(d{2})s(d{2}:d{2}:d{2})/"); $replace = array ('"$1 ".strtoupper("$2")', "$3/$2/$1 $4"); $string = "Posted by John | 2007-02-15 02:43:41"; echo preg_replace($search, $replace, $string);?> Thanks in advance! # Posted by C hillyroll | 11 Jan 2008 23:15:58

webcheatsheet.com/php/regular_expressions.php

4/6

30.10.2013

Using Regular Expressions with PHP


Great, I'm remembering the day when i've searched for the regx. Its really awsome. Great. Very helpful site. So i'm posting two comments in same day. good and great job. no ads here how do you survive.... # Posted by Sumitra Acharya | 17 Jan 2008 06:20:46 Really fantastic step-by-step explanation... Very useful for tyro in Reg exp... Thanks... # Posted by Muthukumaran | 5 Feb 2008 22:17:43 this topic "must seen" thing . its the best php topic, better than official php site, i swear the god. I decided to stop learning php b4 I read this explanation. its kind of tricky stuff i belive. god bless you, and thank you very much. ADDED 2 favorite...OK! you'll c me here all the day. <- so excited. # 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. # Posted by ronair | 27 Mar 2008 02:58:06 This is a realy good explanation of regular expressions. I have been using PHP for quite a while but I have never known how to use regular expressions. C haim C haikin http://chaimchaikin.za.net # Posted by C haim C haikin | 13 Apr 2008 02:00:32 Great tutorial. Made regular expressions easy for me. Thank you so very much from Pune, India. # Posted by Arun | 27 May 2008 13:44:28 Very nice article, 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. */ public void doLinkedListExample() { final int MAX = 10; int counter = 0; System.out.println("+---------------------------------------------------------------------+"); System.out.println("| C reate/Store objects in an LinkedList container. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); List listA = new LinkedList(); List listB = new LinkedList(); for (int i = 0; i < MAX; i++) { System.out.println(" - Storing Integer(" + i + ")"); listA.add(new Integer(i)); } System.out.println(" - Storing String(Alex)"); listA.add("Alex"); System.out.println(" - Storing String(Melody)"); listA.add("Melody"); System.out.println(" - Storing String(Jeff)"); listA.add("Jeff"); System.out.println(" - Storing String(Alex)"); listA.add("Alex"); ========= jonathan ========= <a href="http://fsbo.fastrealestate.net">Homes for sale</a>-Homes for sale # Posted by jonathan | 16 Mar 2009 10:52:47 Excellent script. 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.

webcheatsheet.com/php/regular_expressions.php

5/6

30.10.2013
<?php

Using Regular Expressions with PHP

//Trim space off front and end of Desired Password $dPwd = trim($dPwd); //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/",$dPwd))&&(preg_match("/^.*(?=.{8,})(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$\%*]).*$/", $dPwd))){ echo "Your passwords is strong.";

} else { echo "Your password is weak."; } ?>

# 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.com/v/qgAYasUw6vk"> </param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/qgAYasUw6vk" type="application/x-shockwave-flash" wmode="transparent" width="425" height="366"></embed></object>'; $patterns[0] = "/[Ww]idth="[0-9][0-9][0-9]"/"; $replacements[0] = 'width="400"'; $patterns[1] = "/[Hh]eight="[0-9][0-9][0-9]"/"; $replacements[1] = 'height="400"'; echo preg_replace($patterns,$replacements,$string); # 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 web. Thank you very much! # Posted by Joshua | 22 Jun 2009 14:24:19 Thank you for this article. 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.WebC heatSheet.com All Rights Reserved.

webcheatsheet.com/php/regular_expressions.php

6/6

You might also like