Description

Searches a string for matches to the regular
expression given in pattern in a case-sensitive
way.

Parameters

pattern

Case sensitive regular expression.

string

The input string.

regs

If matches are found for parenthesized substrings of
pattern and the function is called with the
third argument regs, the matches will be stored
in the elements of the array regs.

$regs[1] will contain the substring which starts at
the first left parenthesis; $regs[2] will contain
the substring starting at the second, and so on.
$regs[0] will contain a copy of the complete string
matched.

Return Values

Returns the length of the matched string if a match for
pattern was found in string,
or FALSE if no matches were found or an error occurred.

If the optional parameter regs was not passed or
the length of the matched string is 0, this function returns 1.

Changelog

Version

Description

4.1.0

Up to (and including) PHP 4.1.0 $regs will be
filled with exactly ten elements, even though more or fewer than
ten parenthesized substrings may actually have matched. This has
no effect on ereg()'s ability to match more
substrings. If no matches are found, $regs
will not be altered by ereg().

Examples

Example #1 ereg() example

The following code snippet takes a date in ISO format (YYYY-MM-DD) and
prints it in DD.MM.YYYY format:

User Contributed Notes 23 notes

"the matches will be stored in the elements of the array regs. $regs[1] will contain the substring which starts at the first left parenthesis; $regs[2] will contain the substring starting at the second, and so on. $regs[0] will contain a copy of the complete string matched. "

Beacause By "substring," it means the string contained within the parenthesis.

to make it clear:
^ means begin here (startpoint of ereg)
$ means end here (endpoint of ereg)
* stand for endless characters (I want A-Z, a-z, 0-9 and the other characters to apear from 0 to endless
optional you can write {1,5} for
min 1 times a character or max 5 times a
characters instead of *
{1,2} means min 1 time a character and max 2 times a character (optional you can write * intead of {1,2}
{1} stand for max 1 time a character
\ means from here special characters (like .-_$ and so on)
| means OR

It's important to note for certain regular expression that your $regs[] array will only contain as many values as you have sets of parenthesis in your regexp. (plus 1, of course) For example, the regexp below was designed to locate tags enclosed in <> characters inside a string:

<?php ereg("^([^<]*)(<[^>]+>[^<]*)*$", $str, $tags) ?>

This will *not* create an arbitrarily long array in $tags for the number of tags that it finds. Given the string "This <has> a <couple> of <tags> in it", you'll see:

0 : This <has> a <couple> of <tags> in it1 : This 2 : <tags> in it

Since the same parenthesized section of the regexp is matching all tags found, it overwrites the same section of the array. If you try increasing the number of parenthesis, it just creates duplicates of the same result.

While this is relatively simple example, I was unable find a clean method of doing this anywhere else, so I thought I would post it here.

As part of a file upload package, I wanted to prevent the uploading of double byte character filenames and other special ASCII characters that may not work well on a Windows and/or Linux system. Here is the statement I ended up using which seems to have done the trick.

The expression is deliberately very forgiving of bad HTML - I wanted to match anything that could be reasonably accepted by a forgiving browser, rather than make it standards compliant. Whitespace is allowed between the tagname and the opening and closing tag symbols, and also between the / and the tagname for the closing tag.

For my own use, I have wrapped it in a function call, which you may find useful. Here it is with a few notes. I hope somebody finds it useful.

// Use with eregi to ensure case-insensitive match.// e.g. to split an HTML page based on body tag: // eregi(ereg_MatchedHTMLTags('body'), $Source, $Matches)

// The following values will be held in $Matches//(marked values are unintended byproducts of the expression)// *[0] - the entire string ($Source).// [1] - everything before the opening tag// [2] - the opening tag, including all contents (i.e. everything between < and >)// *[3] - the opening tag from end of the tag name, // e.g. '<body bgcolor="#000000">' gives ' bgcolor="#000000">'// [4] - the tag contents (everything between the opening and closing tag)// [5] - the complete closing tag.// *[6] - the closing tag from the end of the tag name// e.g. '</body invalid text>' gives ' invalid text>'// [7] - everything after the closing tag.

Here is a fixed version of the UK postcode check function by tomas at phusis dot co dot uk. There was a bug on line 2 of the reg expression where a closing square-bracket was doubled-up ("]]" which should've been "]").

I actually used this to validate a phone number. The one discrepancy I found is with patterns such as 503-555-1212, 503-5551212, [503]5551212, and every other pattern that does not have single whitespace or a dash as a delimiter, ereg() will return false when given your expression.

Based on your pattern, I came up with a more general and concise version making use of the dot meta-character:$pattern = "^.?[0-9]{3}.?.?[0-9]{3}.?[0-9]{4}$";

When passed to ereg(), it will return "true" so long as there is 10 digits, regardless of the delimiter chosen by the user.

This is a fix for days over 31 and shorter version^(([1]{1}[0-2]{1}|[0]{0,1}[1-9]{1})[\-]{1}([0-2]{0,1}\d{1|[3]{1,1}[0-1]{1})[\-]{1}[1-2]{1}\d{3}|([0-2]{0,1}\d{1}|[3]{1,1}[0-1]{1})[\-]{1}([1]{1}[0-2]{1}|[0]{0,1}[1-9]{1})[\-]{1}[1-2]{1}\d{3}|[1-2]{1}\d{3}[\-]{1}([1]{1}[0-2]{1}|[0]{0,1}[1-9]{1})[\-]{1}([0-2]{0,1}\d{1}|[3]{1,1}[0-1]{1}))$

Save yourself some headache and time, don't use the \d (digits) \w (alphanumeric) and \s (whitespace) short forms. Not only do they make the code less readable, they don't seem to work with ereg.

Use [0-9], [A-Za-z0-9], [ \n\r\t] instead.

Since the regex example in this article is a bit on the complex side, I'll throw in a simpler regex example:

Say you want to validate valid variable names:

<?php$regex_valid_variable_name = '^[A-Za-z_][A-Za-z0-9_]*$';

// ^ in this context means that the regex is anchored// to the beginning of the string.//// A single [xxx] means that a single letter must mach// the criteria within//// The [xxx]* means that [xxx] can mach from zero to// unlimited times.//// The $ is another anchor, except it is for the end of // the sting.

Ok well someone else posted this but if didn't work so I made my own.I used this to check file names that are to be created on a server.File names that start with a-Z or 0-9 and contain a-Z, 0-9, underscore(_), dash(-), and dot(.) will be accepted.File names beginning with anything but a-Z or 0-9 will be rejected.File names containing anything other than above mentioned will also be rejected.

16-Feb-2005 10:02attempts to allow square brackets in a string with^[a-zA-Z0-9 [.[.] [.].] ]{1,}$Although this appears to work a less confusing means is^[]a-zA-Z0-9[]{1,}$The ] has to be the first character (after a possible ^) but the [ can beanywhere as long as it is not in the middle of a range of course.

09-Apr-2005 11:52Says that ereg("hi[:space:]*bob", $string)doesnt work in php 4 and to use preg_match() instead.

The above quoted use is incorrect it should be<?php ereg("hi[[:space:]]*bob", $string); ?>

23-May-2005 08:22Says that ereg("^[' A-Za-Z]+$", $cardName); will not work.

The fault with the above is the range a-Z the capital Z comes before small aand so this will fail. The following works fine<?php$cardname = "John 'Doe'";$result = ereg("^[' A-Za-z]+$", $cardname, $arr);echo ('Matches '.$result.' characters');//Prints Matches 10 characters?>

for example: if you send e-mail to myemail+something@gmail.com, gmail reroutes it to the account myemail@gmail.com. This is a useful feature, and it is very frustrating when websites that take your e-mail don't allow you to do this.