Regular Expressions are notoriously difficult to get started with. This tutorial has been simplified as much as possible in natural progression to avoid complications when you are trying to apply regex to your own programs.
The ereg function simply says if it found whatever you're searching for.
Example 1:
ereg("intelligence", "Fox News!");
This statement will return "0" for, no, intelligence was not found in "Fox News'.
Example 2:
ereg("ass", "The assistant assassinated assignment.");
This returns true because there are 4 instances of "ass". It's something to keep in mind while adding a curse filter.
Example 3:
ereg("Cry", "Bob bought crystals.");
What does this return? True or false?
The function returns false, because ereg is a "case sensitive" function. This means that lower case and upper case letters are evaluated for equality when compared. eregi, is the case insensitive version. strtolower(); is a function that makes strings lowercase.
You want to see if "http://" is included in a URL automatically.
<?$url="www.jimmyr.com";if (ereg("http://", $url)){echo "It has an http://";}?>Let's break it down.
<?$url="www.jimmyr.com";// The url is defined as a string, notice it does not have http://if (ereg("http://", $url)){// ereg("http://", $url) asks "Does the variable $url contain 'http://'?"// if yes, then it will return a value of 1, if no, then 0echo "It has an http://";}?>Suppose you want to check if the url starts with http. To evaluate the start of the expression we use the caret caret (^) at the start.
Solution 1: ereg("^http", $url);
Now I want to check if the url ends with a .com . To evaluate endings we use the dollar sign at the end of the expression. Certain characters can produce problems. The period character, ".", matches any character one time. In this case if we use ereg(".com$", $url); www.wirelesscom.net will return true, because, "scom" will match ".com". We have to add an escape character (\) before the period in order for the program to take it literally.
Solution 2: ereg("\.com$", $url);
Suppose I want to search for the word "colors", but some people spell it "colours". This can be accomplished with a question mark. The question mark says the "u" is optional.
Solution 3: ereg("colors", "colou?rs");
But what if you want to match pictures and some people abbreviate it pics?? ereg("picture?s", "pics"); would only return true on pictur_s and pics.
Solution 4: ereg("pic(ture)?s", "pics");
What about something that says "does not contain". For example. An email address. Lets see make sure it doesn't contain |,!, and ?. To do this, we box box the characters and indicate that we don't want any of them by putting a caret after the [. Note that the "|" character must be escaped.
Solution 5: ereg("[^@\|!]", "$email");
Say I want it to match a 3 images. The basic format for an image is filename.extention. For example: me.gif, heftysue.jpg, and cows.bmp. The logic is: It starts with: me, heftysue, or cows in the beginning. Then we have a period. So, "\.". Then finally the end is either gif, jpg, or bmp. Do you remeber the function for OR in IF functions? if (($I=="cool) || ($I=="rich")){ $I="rule"; } Parenthesis are always used when evaluating multiple phrases. We will use parenthesis again and denote OR with a "|".
Solution 6: ereg("^(me|heftysue|cows)\.(jp(e)?g|gif|bmp)$", "$img");
Say I wanted to see automatically if a person is in the age range of 25-27. The age is stored in variable $age. There are two ways of doing this. Brackets are used when multiple values can match one spot, in this example, the second digit, which can be 5,6, or 7. A range is denoted by a dash.
Solution 7: ereg("colors", "2[567]"); OR ereg("colors", "2[5-7]");
Characters also work the same way. If I wanted to see if a variable was a, b or c.
Solution 8: ereg("^[a-c]", "$character");
I want to see if the character is a-c or 0-9.
Solution 9: ereg("^[0-9a-c]", "$character");
You want to match something several times only three times. For examples, some people write an elipsis(...) incorrectly as .., ....., ......, etc. You want to change it to 3 dots.
Solution 10: ereg_replace("[\.]{2,99}","...", "$message");
You decide two dots are exceptable as well, but otherwise, change it to 3 dots.
Solution 11: ereg_replace("[\.]{4,99}","...", "$message");
You don't like anything more than 1 dot.
Solution 12: ereg_replace("[\.]+", "\.", "$message");Note that * matches any number of times, including 0.
Start and ending characters are ^ and $.
Question marks mean that whatever precedes is optional.
Brakets denote a range.
Backslashes are escape characters
It's not important you memorize all these, however, it is important you know where and how to reference them.
Both of these functions preform the same task but in different ways. The only difference is that ereg replace is slightly slower than preg_replace.