PHP-Regular Expressions

Think of regular expressions as an elaborate system of matching patterns. You first write the pattern, then use one of PHP's built-in functions to apply the pattern to a text string regular expressions are specifically for use with strings. PHP has essentially two functions for using regular expressions to match patterns one case sensitive and one not and two for matching patterns and replacing matched text with other text again, one case sensitive and one not

Some text editors, such as BBEdit for Macintosh, TextPad for Windows and emacs for Unix, allow you to use regular expressions to match and replace patterns within and throughout several documents.
This may be another good reason to learn regular expressions and is perhaps something to consider when choosing your text editor.


This is a fairly complete list of special characters used to define your regular expression patterns (including metacharacters but not literals—a, b, c, etc.).
Special Characters for Regular Expression
Character Matches
. any character
^a begins with a
a$ ends with a
a+ at least one a
a? zero or one a
\n new line
\t tab
\ escape
(ab) ab grouped
a|b a or b
a{ 2} aa
a{ 1,} a, aa, aaa, etc.
a{ 1,3} a, aa, aaa
[a-z] any lowercase letter
[A-Z] any uppercase letter
[0-9] any digit
Regular expressions also make use of the pipe | as the equivalent of or. Therefore, "a|b" will match the strings a or b and "gre|ay" matches both potential spellings of the color. Using the pipe within patterns is called alternation.

Practically, of course, there's little use to matching repetitions of a letter in a string, but these examples are good ways to demonstrate how a symbol works. You should begin by focusing on understanding what the various symbols mean and how they are used. 

To include special characters (^.[]$()|*?{ } \) in a pattern, they need to be escaped (a backslash put before them). This is true for the metacharacters and the grouping symbols (parenthesis and brackets). You can also use the backslash to match new lines ("\n") and tabs ("\t"), essentially creating a metacharacter out of a literal.


There are two functions built in to PHP expressly for the purpose of matching a pattern within a string: ereg() and eregi(). The only difference between the two is that ereg() treats patterns as case-sensitive whereas eregi() is case-insensitive, making it less particular. The latter is generally recommend for common use, unless you need to be more explicit (perhaps for security purposes, as with passwords). Both functions will be evaluated to TRUE if the pattern is matched, FALSE if it is not. Here are two different ways to use these functions:
ereg("pattern", "string");
Or:
$Pattern = "pattern";
$String = "string";
eregi($Pattern, $String);
Throughout the rest of the chapter, I will assign the pattern to a variable, as in the second example above, to draw more attention to the pattern itself—the heart of any regular expression.
Related Posts:
  • XMLHttpRequest-Ajax To do this, you must understand the three ways of creating an XMLHttpRequest object• IE 5: request = new ActiveXObject("Microsoft.XMLHTTP")• IE 6+: request = new ActiveXObject("Msxml2.XMLHTTP")• All others: request = new XML… Read More
  • PHP Curl check for file existence a PHP program that downloads a pdf from a backend and save to a local drive. $url = "http://wedsite/test.pdf"; $path = "C:\\test.pdf;" downloadAndSave($url,$path); function downloadAndSave($urlS,$pathS) { $fp… Read More
  • Php tutorial-SYMBOLS ! (logical operator)!= (comparison operator)!= (inequality operator)!== (comparison operator)!== (non-identity operator)$result->fetch_assoc() function$type parameter% (modulus operator)% (wildcard character)%= (combined … Read More
  • PHP error: Cannot modify header information You cannot use header() once text has been output to the browser. As your header.php include presumably outputs HTML, header() cannot be used. You can solve this in a couple ways: Move the if statement above the heade… Read More
  • Download file using curl in php You would need to feed CURLOPT_URL the full URL to the file. Also if you want to download a file you might want to save it somewhere. Working example: $curl = curl_init(); $file = fopen("ls-lR.gz", 'w'); curl_setopt… Read More
  • IT company in kolkata Matrix Technologies Pvt. Ltd.8/1C, Chowringhee Lane, Room No. 2E, 2nd flKolkata7000162252 8/29 Media Software61A, S.N.Roy RdKolkata70003424479580 Metalogic Systems Pvt. LtdPlot-J1/1, Block-EP and GP, Sector-V, Salt LakeK… Read More
  • Server Side Includes A server-side include is a coding that you can include within your HTML document that will tell the web server to include other information with the document being served.    Server side includes are a handy w… Read More
  • Php Operators PHP Operators The following table lists the operators from highest to lowest precedence. Operators A !, ~, ++, --, @ Right *, /, % Left +, -, . Left <<, >> Left <… Read More
  • php-static variables A static variable retains its value between calls to a function but is visible only within that function. IT declare a variable static with the static keyword. function test_counter ( ) { static $counter = 0; $counte… Read More
  • Drupal Bootstrap Process Drupal Bootstrap Process? Drupal bootstraps itself on every request by going through a series of bootstrap phases. These phases aredefined in bootstrap.inc and proceed. Configuration Sets global variables used throughout th… Read More
  • Php Networking Functions There are many functions in php to convert or look up domain name, IP address, protocol, and service information. Domain name/IP address lookups and conversions gethostbynamel() gethostbyname() gethostbyaddr() … Read More
  • pass information from page to page-best seo Adding information to the URL: You can add certain information to the end of the URL of the new page, and PHP puts the information into builtin arrays that you can use in the new page. This method is most appropriate when yo… Read More
  • PHP: How do I enable error reporting? he following enables all errors: ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); See http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors http://php.net/manu… Read More
  • What Is a WAMP, MAMP, or LAMP? WAMP, MAMP, and LAMP are abbreviations for Windows, Apache, MySQL, andPHP,Mac, Apache, MySQL, and PHP, and Linux, Apache, MySQL, and PHP.These abbreviations describe a fully functioning setup used for developing dynamicInter… Read More
  • php-Error Control Operators PHP gives one error control operator: the at sign (@).Any error messages that might be generated by that expression will be ignored. If you have set a custom error handler function with set_error_handler() then it will s… Read More