My rendition of a function that validates email addresses.
<?php
function validate_EmailAddress($email) {
return
is_string($email) &&
!empty($email) &&
eregi("^[a-z0-9_-]+[a-z0-9_.-]*@[a-z0-9_-]+[a-z0-9_.-]*\.[a-z]{2,5}$", $email);
}
?>
ereg
(PHP 4, PHP 5)
ereg — Recherche par expression rationnelle standard
Description
Recherche dans la chaîne string les séquences de caractères qui correspondent au masque pattern, en tenant compte de la casse.
Cette fonction est OBSOLETE depuis PHP 5.3.0. Nous vous encourageons vivement à ne plus l'utiliser.
Liste de paramètres
- pattern
-
Expression rationnelle sensible à la casse.
- string
-
La chaîne d'entrée.
- regs
-
Si au moins une séquence est trouvée (éventuellement dans les parenthèses capturantes de pattern), et que la fonction est appelée avec un troisième argument regs, les résultats seront enregistrés dans regs.
$regs[1] contiendra la première parenthèse capturante (celle qui commence le plus tôt), $regs[2] contiendra la deuxième parenthèse capturante (celle qui commence après la première), et ainsi de suite. $regs[0] contient une copie de la chaîne.
Valeurs de retour
Retourne la longueur de l'occurrence trouvée si une occurrence a été trouvée dans la chaîne string et FALSE dans le cas contraire ou si une erreur est survenue.
Si le paramètre optionnel regs n'est pas passé à la fonction ou si la longueur du paramètre string vaut 0, la fonction retournera 1.
Historique
| Version | Description |
|---|---|
| 4.1.0 | Pour les versions de PHP inférieures ou égales à 4.1.0, $regs sera rempli d'exactement dix éléments, même s'il y a plus ou moins de dix parenthèses capturantes. Cela n'a aucun effet sur ereg() pour chercher plus de sous-chaînes. Si aucun résultat n'est trouvé, $regs ne sera pas altéré par ereg(). |
Exemples
Exemple #1 Exemple avec ereg()
L'exemple suivant prend une date au format ISO (YYYY-MM-DD) et l'affiche sous la forme DD.MM.YYYY :
<?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 "Format de date invalide : $date";
}
?>
Notes
Note:
À partir de PHP 5.3.0, l'extension regex est obsolète et vous devez utiliser l'extension PCRE à la place. Appeler des fonctions de l'extension regex émettra des alertes de type E_DEPRECATED. Voir la liste des différences pour vous aider dans la conversion en PCRE.
Note:
preg_match(), qui utilise la syntaxe des expressions rationnelles compatibles PERL, est une alternative plus rapide de ereg().
Voir aussi
- eregi() - Recherche par expression rationnelle insensible à la casse
- ereg_replace() - >Remplacement par expression rationnelle
- eregi_replace() - Remplacement par expression rationnelle insensible à la casse
- preg_match() - Expression rationnelle standard
- strpos() - Cherche la position de la première occurrence dans une chaîne
- strstr() - Trouve la première occurrence dans une chaîne
- quotemeta() - Protège les métacaractères
email validation, based on previous script:
<?php
function validateMail($mail) {
if($mail !== "") {
if(ereg("^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$", $mail)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
?>
Remember, from "if(ereg..." to "$mail)) {" it's ONE line. Dont brake it. =)
Thanks to Parsaj.
Here a little clear and easy script
that I wrote to validate an E-Mail Input.
I tried to understand ereg,
but the describtion on php.net did not help me,
so I made some research and finally (think)
that Im sure how to use ereg.
The following function works for me well and allows
an E-Mail to contain the following characters:
"A-Z, a-z, 0-9, . , - , _ "
<?php
function validateMail($mail) {
if($mail !== "") {
if(!ereg("^[A-Za-z0-9\.|-|_]*
[@]{1}[A-Za-z0-9\.|-|_]*[.]{1}[a-z]{2,5}$", $mail)) {
echo "wrong Input for E-Mail";
$mail ="";
return $mail;
}
else {
return $mail;
}
}
}
?>
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
Recognizing E-mail Addresses
worth noting: a valid e-mail may contain a + character.
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.
Hi,
for those looking for email adress PHP validation,
isemail is a javascrit function that checks the following conditions for the email string:
• only one @ sign permitted
• domain extension separator (.) must come after the @ symbol
• must be at least 2 characters (letters) after the domain extension separator (.)
• rejects all illegal characters including spaces.
• Allows only numbers, letters, the underscore (_) and the dash (-) character as valid input
(excluding the mandatory "@" and "." symbols).
• Minimum of 6 characters
The following pattern satisfy all-in-one for the ereg function:
$pattern = "^([A-Za-z0-9\.|-|_]{1,60})([@])";
$pattern .="([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,3})$";
ereg($pattern,$email)
Here is a series of useful regular expressions.
<?php
function isValid($type,$var) {
$valid = false;
switch ($type) {
case "IP":
if (ereg('^([0-9]{1,3}\.){3}[0-9]{1,3}$',$var)) {
$valid = true;
}
break;
case “URL”:
if (ereg("^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu)$",$var)) {
$valid = true;
}
break;
case “SSN”:
if (ereg("^[0-9]{3}[- ][0-9]{2}[- ][0-9]{4}|[0-9]{9}$",$var)) {
$valid = true;
}
break;
case “CC”:
if (ereg("^([0-9]{4}[- ]){3}[0-9]{4}|[0-9]{16}$",$var)) {
$valid = true;
}
break;
case “ISBN”:
if (ereg("^[0-9]{9}[[0-9]|X|x]$",$var)) {
$valid = true;
}
break;
case “Date”:
if (ereg("^([0-9][0-2]|[0-9])\/([0-2][0-9]|3[01]|[0-9])\/[0-9]{4}|([0-9][0-2]|[0-9])-([0-2][0-9]|3[01]|[0-9])-[0-9]{4}$",$var)) {
$valid = true;
}
break;
case “Zip”:
if (ereg("^[0-9]{5}(-[0-9]{4})?$",$var)) {
$valid = true;
}
break;
case "Phone":
if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$",$var)) {
$valid = true;
}
break;
case “HexColor”:
if (ereg('^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$',$var)) {
$valid = true;
}
break;
case “User”:
if (ereg("^[a-zA-Z0-9_]{3,16}$",$var)) {
$valid = true;
}
break;
}
return $valid;
}
#Example:
$phone = "789-1234";
if (isValid("Phone",$phone)) {
echo "Valid Phone Number";
} else {
echo "Invalid Phone Number";
}
?>
In reply to Francis,
Your regular expression:
$pattern = "^(\()?([0-9]{3})(\))?( )?([0-9]{3})( )?(\-)?( )?([0-9]{4})$";
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.
[503] 5551212 //true
5035551212 //true
503+555 1212//true
$pattern = "^(\()?([0-9]{3})(\))?( )?([0-9]{3})( )?(\-)?( )?([0-9]{4})$";
UK postcode validation.
We've used the expression put forward by tomas and jaik,
but we found that it fails on Coventry (CV) postcodes.
Here is an alternative that works for these postcodes and
also adds support for the Overseas Territories like Tristan da Cunha.
<?php
function IsPostcode($postcode) {
$postcode = strtoupper($postcode);
if(ereg("((GIR 0AA)|(TDCU 1ZZ)|(ASCN 1ZZ)|(BIQQ 1ZZ)|(BBND 1ZZ)"
."|(FIQQ 1ZZ)|(PCRN 1ZZ)|(STHL 1ZZ)|(SIQQ 1ZZ)|(TKCA 1ZZ)"
."|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]"
."|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))"
."|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})", $postcode)) {
return $postcode;
} else {
return FALSE;
}
}
?>
<?php
function fractionstr2decimal($fs){
if (is_numeric($fs)){
return $fs;
} elseif (ereg ("([0-9]{1,6}[ ])?([0-9]{1,6})\/([0-9]{0,6}[1-9][0-9]{0,6})", $fs, $regs)) {
/* ({1-6 digits}space optional) {1-6 digits}/(1-13 digits with at least one digit not 0 )*/
return ($regs[1]+$regs[2]/$regs[3]);
} else {
return (NULL); /* Invalid Fraction */
}
}
echo fractionstr2decimal("1024 5/132")."<br>"; /* 1024.0378787879 */
echo fractionstr2decimal("23")."<br>"; /* 23 */
$i=fractionstr2decimal("1 2/3");
echo "<br>$i"; /* 1.66666666666667 */
if ($i and $i<=1 ) echo "<1<br>"; /* */
$i=fractionstr2decimal("2/3");
echo "<br>$i"; /* 0.66666666666667 */
if ($i and $i<=1 ) echo "<1<br>"; /* <1 */
$i=fractionstr2decimal("2/0");
echo "<br>$i";
if ($i and $i<=1 ) echo "<1<br>";
?>
I noticed that /s which is used to check for spaces doesn't work with ereg. Not sure why. Here's an example of a US phone validation function that works for me. I couldn't find a working example anywhere, so here it is.
<?
function invalid_phone($phone_no){
$pattern = "^(\()?([0-9]{3})(\))?( )?([0-9]{3})( )?(\-)?( )?([0-9]{4})$";
$phone_no = trim($phone_no);
if (!ereg($pattern, $phone_no)) return 1;
return 0;
} // invalid_phone
// example of use
$phone = "(503) 555-1212"; // ok
$phone = "(503) 555 - 1212"; // ok
$phone = "503-555-1212"; // ok
$phone = "503 - 555 - 1212"; // ok
$phone = 5035551212; // ok
if (invalid_phone($phone) > 0) echo "Make sure the phone number you have entered matches the standard format: (503) 555-1212";
?>
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 it
1 : 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.
There is undocumented behaviour for this command!
You cannot check for strings of 1-256 characters or longer.
For example, this will evaluate to true:
ereg("^[a-zA-Z0-9' ]{1,255}$","ghfhg");
But this will give the error:
PHP Warning: ereg(): REG_BADBR
ereg("^[a-zA-Z0-9' ]{1,256}$","ghfhg");
PHP version (on Windows XP):
PHP 5.2.5 (cli) (built: Nov 8 2007 23:18:51)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
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}))$
This is a date regexp that I made to allow different combinations of month, day, and year. it's rather long but it works.
(^([0-3]{0,1})([1-9]{1,1})([\-]{1,1})(([1]{1,1})([0-2]{1,1})|
([0]{0,1})([1-9]{1,1}))([\-]{1,1})([1-2]{1,1})(\d{3})$)|
(^(([1]{1,1})([0-2]{1,1})|([0]{0,1})([1-9]{1,1}))([\-]{1,1})
([0-3]{0,1})([1-9]{1,1})([\-]{1,1})([1-2]{1,1})(\d{3})$)|
(^([1-2]{1,1})(\d{3})([\-]{1,1})(([1]{1,1})([0-2]{1,1})|
([0]{0,1})([1-9]{1,1}))([\-]{1,1})([0-3]{0,1})([1-9]{1,1})$)
yyyy-mm-dd, mm-dd-yyyy, dd-mm-yyyy
2007-02-05
12-25-2007
25-02-2007
2-5-2007
25-2-2007
2007-9-23
09-06-2007
09-23-2007
The above will check valid
Anything not in one of the combinations of dd,mm,yyyy formats above will fail.
these will also fail
2007-32-12
13-22-2007
0-1-2007
1-0-2007
Any sugestions on making it smaller and more efficients would be appreciated.
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.
// Valid names: "_", "hello1", "a_variable"
// Invalid names: "4number", "five-to", "one two", " space "
//Test it out:
$regx = $regx_valid_variable_name;
$valid = array ( '_', 'hello1', 'a_variable' );
$invalid = array ( '4number', 'five-to', 'one two', ' space ');
foreach($valid as $v)
echo 'Valid '.(ereg($regx, $v) ? 'yes' : '<b>no</b>') . ": $v<br />\n";
foreach($invalid as $v)
echo 'Invalid '.(!ereg($regx, $v) ? 'yes' : '<b>no</b>') . ": $v<br />\n";
?>
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 "]").
<?php
function IsPostcode($postcode) {
$postcode = strtoupper(str_replace(chr(32),'',$postcode));
if(ereg("^(GIR0AA)|(TDCU1ZZ)|((([A-PR-UWYZ][0-9][0-9]?)|"
."(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|"
."(([A-PR-UWYZ][0-9][A-HJKSTUW])|"
."([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))"
."[0-9][ABD-HJLNP-UW-Z]{2})$", $postcode))
return $postcode;
else
return FALSE;
}
?>
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.
Here it is.
<?php
$result = ereg("(^[a-zA-Z0-9]+([a-zA-Z\_0-9\.-]*))$" , $filename);
?>
I could not find a definitive and 100% working function that validates the UK postcodes, so was forced to write one myself.
The authoritative source of information is
http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm
which I amended with the new postcode for Tristan da Cunha.
Here is the ugly beast (don't wanna see regexp's ever again):
<?php
function IsPostcode($postcode) {
$postcode = strtoupper(str_replace(chr(32),'',$postcode));
if(ereg("^(GIR0AA)|(TDCU1ZZ)|((([A-PR-UWYZ][0-9][0-9]?)|"
."(([A-PR-UWYZ][A-HK-Y]][0-9][0-9]?)|"
."(([A-PR-UWYZ][0-9][A-HJKSTUW])|"
."([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))"
."[0-9][ABD-HJLNP-UW-Z]{2})$", $postcode))
return $postcode;
else
return FALSE;
}
?>
Try this version instead of the one previously posted.
<?php
/**
Returns an array containing each of the sub-strings from text that
are between openingMarker and closingMarker. The text from
openingMarker and closingMarker are not included in the result.
This function does not support nesting of markers.
*/
function returnSubstrings($text, $openingMarker, $closingMarker) {
$openingMarkerLength = strlen($openingMarker);
$closingMarkerLength = strlen($closingMarker);
$result = array();
$position = 0;
while (($position = strpos($text, $openingMarker, $position)) !== false) {
$position += $openingMarkerLength;
if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) {
$result[] = substr($text, $position, $closingMarkerPosition - $position);
$position = $closingMarkerPosition + $closingMarkerLength;
}
}
return $result;
}
// Example:
$exampleText = "<b>bonjour</b> à tous, <b>comment</b> allez-vous ?";
$result = returnSubstrings($exampleText, "<b>", "</b>");
var_export($result);
// Prints:
// array (
// 0 => 'bonjour',
// 1 => 'comment',
// )
?>
Here's a function i've created to return an array of each substring searched in a string.
<?
function Return_Substrings($text, $sopener, $scloser)
{
$result = array();
$noresult = substr_count($text, $sopener);
$ncresult = substr_count($text, $scloser);
if ($noresult < $ncresult)
$nresult = $noresult;
else
$nresult = $ncresult;
unset($noresult);
unset($ncresult);
for ($i=0;$i<$nresult;$i++)
{
$pos = strpos($text, $sopener) + strlen($sopener);
$text = substr($text, $pos, strlen($text));
$pos = strpos($text, $scloser);
$result[] = substr($text, 0, $pos);
$text = substr($text, $pos + strlen($scloser), strlen($text));
}
return $result;
}
?>
Example :
<?
$string = "<b>bonjour</b> à tous, <b>comment</b> allez-vous ?";
$result = Return_Substrings($string, "<b>", "</b>");
?>
I wanted a more strict check for UK postcodes, and decided to do it by stripping all whitespace then using ereg:
<?php
$pcode=str_replace(" ","",$in_post_code);
if (!ereg('^[a-zA-Z]{1,2}[0-9]{1,2}[a-zA-Z]{0,1}[0-9]{1}[a-zA-Z]{2}$', $pcode))
{
return false;
}
?>
Probably could be improved, as I've just started, but it matches everything listed on the post office spec.
I had problem using is_numeric() to verify if user inputs is a number (including optional floating sign and decimals). Instead I found this expression from http://www.regular-expressions.info/floatingpoint.html and modified it for a bit.
^[+-]?[0-9]*\.?[0-9]+$
/*
3.55 true
-3.55 true
+3.55 true
2456.90 true
34skd false
23. false
2dt6 false
*/
Note: mine doesn't have the exponent part; for matching number with exponents, visit the site above :)
Hello
I think this is not clear:
"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.
But in that statement it isn't so clearly
With regards
Amir Hossein Estakhrian
A common mistake seems to be trying to escape characters within a bracket
expression. Unlike the preg functions, backslash is always taken literally
within a bracket expression using the ereg functions. See
http://php.planetmirror.com/manual/en/function.eregi.php#57824
for more details.
Some of the posts here can be re-written to be much simpler.
16-Feb-2005 10:02
attempts 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 be
anywhere as long as it is not in the middle of a range of course.
09-Apr-2005 11:52
Says 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); ?>
I tested this with the following in php 4.3.3 and it works fine
<?php
//The hex codes are space, tab, line feed, vertical tab, form feed, carriage return
$whitespace = "\x20\x09\x0a\x0b\x0C\x0d";
$teststring = "hi".$whitespace."bob";
$result = ereg ("hi[[:space:]]*bob", $teststring, $arr);
echo ('Matches '.$result.' characters');
//Prints Matches 11 characters
?>
23-May-2005 08:22
Says 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 a
and 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
?>
09-Sep-2005 11:01
Tries to escape with \ in a bracket expression
You cannot with ereg functions (preg you can) so
ereg("^([-a-zA-Z0-9_\.\!@#\$&\*\+\=\|])*$" , $var)
should be
<?php ereg("^([-a-zA-Z0-9_.!@#$&*+=|])*$", $var); ?>
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.
ereg("[^a-zA-Z0-9._-]", $file_name)
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(>|[^>]*>))(.*)$";
}
// 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.
?>
