|
|
 |
ereg (PHP 3, PHP 4, PHP 5) ereg -- Regular expression match Descriptionint ereg ( string pattern, string string [, array ®s] ) Note:
preg_match(), which uses a Perl-compatible
regular expression syntax, is often a faster alternative to
ereg().
Searches a string for matches to the regular
expression given in pattern in a case-sensitive
way.
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.
Note:
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().
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.
The following code snippet takes a date in ISO format
(YYYY-MM-DD) and prints it in DD.MM.YYYY format:
Example 1. ereg() example |
<?php
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo "$regs[3].$regs[2].$regs[1]";
} else {
echo "Invalid date format: $date";
}
?>
|
|
See also eregi(), ereg_replace(),
eregi_replace(), preg_match(),
strpos(), and strstr().
User Contributed Notes
ereg
berndt at michael berndt dot de
29-Apr-2005 08:23
php at REMOVEMEkennel17 dot co dot uk
26-Apr-2005 09:00
After a lot of hard work I managed to create the following regular expression, which matches any HTML tag pair (i.e. opening and closing tag), as specified by tagname:
^(.*)(<[ \n\r\t]*tagname(>|[^>]*>))(.*)(<[ \n\r\t]*/[ \n\r\t]*tagname(>|[^>]*>))(.*)$
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.
- Mark Clements
<?php
function ereg_MatchedHTMLTags($tagname) {
return "^(.*)(<[ \\n\\r\\t]*$tagname(>|[^>]*>))(.*)(<[ \\n\\r\\t]*/[ \\n\\r\\t]*$tagname(>|[^>]*>))(.*)$";
}
?>
kitchen -at- script kitchen -dot- com
08-Apr-2005 08:52
just to note here, php4's ereg() doesn't support [:space:], so if you want to match arbitrary whitespace, use preg_match()
$string = "hi \t bob";
if (ereg("hi[:space:]*bob", $string)) {
print "you must be using php5!\n";
}
// php4 version
if (preg_match("/hi\s*bob/",$string)) {
print "works in both php4 and php5!\n";
}
php5's ereg() does support [:space:]
alcator at seznam dot cz
11-Mar-2005 01:47
Reaction to:
(19[0-9]{2}|20[0-5]{2})-(0[0-9]|1[0-2])-([12][0-9]|3[01])
--> There is more problems than just "this will validate 2005-02-31". The worse problem is that it WON'T validate 2001-01-01...
(19[0-9]{2}|200[0-5]{1})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])
(Note that "00" is no longer possible for days and months, while allowing for 01..09...)
Still, it doesn't protect against non-existent dates.
Candle1deleteme2121 at DELETEMEhotmail dot com
07-Mar-2005 05:36
LIX
01-Mar-2005 04:13
It's important to know - when you use
[0-9]{4})-([0-9]{2})-([0-9]{2}
to valid date in YYYY-MM-DD format then script accepts dates etc. 20005-02-035
You can use
^[0-9]{4})-([0-9]{2})-([0-9]{2}$
to valid regular date format YYYY-MM-DD.
24-Feb-2005 02:17
The date example above is still a little dodgey as this:
(19[0-9]{2}|20[0-5]{2})-(0[0-9]|1[0-2])-([12][0-9]|3[01])
will allow invalid dates such as
1900-00-31
Just a small correction is needed (IMO), switching a 0 for a 1:
(19[0-9]{2}|20[0-5]{2})-(0[1-9]|1[0-2])-([12][0-9]|3[01])
^
Of course invalid dates such as 31st feb are still possible.
punkpuke at comcast dot net
15-Feb-2005 06:02
This took me a little to find out, but from one PHP/Regex newb to another if your trying to let people use Square Brackets in a string: [ ]
Like in this string for example: ^[a-zA-Z0-9]{1,}$
This following will most likely not work (You can't backslash them): ^[a-zA-Z0-9\[\]]{1,}$
So, to escape the Square Brackets, use the following to delimit collating symbols:
[. Special Character Goes Here .]
So The New Expression: ^[a-zA-Z0-9 [.[.] [.].] ]{1,}$ (I spaced it so it was less confusing)
rhondle at hotmail dot com
13-Feb-2005 05:56
Carrajola
12-Feb-2005 12:54
Since the example has the purpose of validating the date in $date and not just change it, the regular expression is not accurate, because a date like 0000-00-00 is validated.
It should be something like this:
(19[0-9]{2}|20[0-5]{2})-(0[0-9]|1[0-2])-([12][0-9]|3[01])
this regular expression is still not totally correct because it's not sensible to a impossible date like 2005-02-31.
| |