PHP Doku:: Sucht Übereinstimmung mit regulärem Ausdruck ohne Berücksichtigung von Groß-/Kleinschreibung - function.eregi.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzTextverarbeitungReguläre Ausdrücke (POSIX-erweitert)POSIX Regex-Funktioneneregi

Ein Service von Reinhard Neidl - Webprogrammierung.

POSIX Regex-Funktionen




(PHP 4, PHP 5)

eregiSucht Übereinstimmung mit regulärem Ausdruck ohne Berücksichtigung von Groß-/Kleinschreibung


int eregi ( string $pattern , string $string [, array &$regs ] )

Diese Funktion entspricht ereg() mit dem Unterschied, dass sie übereinstimmende Buchstaben nicht nach Groß- und Kleinschreibung unterscheidet.


Diese Funktion ist seit PHP 5.3.0 DEPRECATED (veraltet). Sich auf diese Funktion zu verlassen ist in keiner Weise empfehlenswert.



Regulärer Ausdruck ohne Berücksichtigung der Groß- und Kleinschreibung


Die zu durchsuchende Zeichenkette


Wenn Übereinstimmungen mit eingeklammerten Teilzeichenketten von pattern gefunden werden und die Funktion mit dem dritten Argument regs aufgerufen wurde, werden die Übereinstimmungen in den Elementen des Arrays regs gespeichert.

$regs[1] enthält die Teilzeichenkette der ersten Klammer, $regs[2] die Teilzeichenkette der zweiten usw. $regs[0] enthält bei Übereinstimmung mit string eine Kopie der kompletten Zeichenkette string.


Gibt die Länge der übereinstimmenden Zeichenkette zurück, falls in string eine Übereinstimmung mit pattern gefunden wurde oder FALSE, wenn keine Übereinstimmung gefunden wurde oder wenn ein Fehler aufgetreten ist.

Falls der optionale Parameter regs nicht übergeben wurde oder die Länge der übereinstimmenden Zeichenkette 0 ist, gibt diese Funktion 1 zurück.


Beispiel #1 eregi()-Beispiel

if (
eregi('z'$string)) {
"'$string' contains a 'z' or 'Z'!";



Seit PHP 5.3.0 ist die regex-Erweiterung zugunsten der PCRE-Erweiterung als veraltete markiert. Ein Aufruf dieser Funktion wird eine E_DEPRECATED-Notice ausgeben. Sie können sich die Liste der Unterschiede ansehen, wenn Sie Hilfe beim Umstieg auf PCRE benötigen.

Siehe auch

  • ereg() - Sucht Übereinstimmungen mit einem regulären Ausdruck
  • ereg_replace() - Ersetzt einen regulären Ausdruck
  • eregi_replace() - Ersetzt einen regulären Ausdrück ohne Berücksichtigung von Groß-/Kleinschreibung
  • stripos() - Findet das erste Vorkommen eines Strings, unabhängig von Groß- und Kleinschreibung
  • stristr() - Wie strstr, aber unabhängig von Groß- bzw. Kleinschreibung

50 BenutzerBeiträge:
- Beiträge aktualisieren...
info at o08 dot com
15.04.2010 6:12
because eregi is not recommended after php 5, you can replaced it with stristr if just for simple search.

For editors with regular express function:
julia_kean25 at yahoo dot com
10.02.2009 23:15
I use this in my app_config.php file to sanitize each request:

// app_config.php


function sanitize_request($methods, $array)
// methods: trim ; addslashes ; stripslashes ; etc...
        // array : $_GET ; $_POST ; etc...

foreach ($methods as $function) {
$array = array_map($function, $array);

    if ( !
get_magic_quotes_gpc() )
$methods = array('trim', 'addslashes');
$_GET = sanitize_request($methods, $_GET);
$_POST = sanitize_request($methods, $_POST);
$_COOKIE = sanitize_request($methods, $_COOKIE);
$_REQUEST = sanitize_request($methods, $_REQUEST);

it currently only trims and adds slashes to the request but it would be nice to have the possibility to add the striptags function too.
jrgpmaster at gmail dot com
13.09.2008 10:04
Here is a simple way of checking if the visitor if your page is a search engine or a normal person. It does this by checking if the user agent returned by $_SERVER['HTTP_USER_AGENT'] contains one of the keywords search engine's user agents usually contain.


//check if user is a bot of some sort
function is_bot()
$bots = array('google','yahoo','msn');
//takes the list above and returns (google)|(yahoo)|(msn)
$regex = '('.implode($bots, ')|(').')';
/*uses the generated regex above to see if those keywords are contained in the user agent variable*/     
return eregi($regex, $_SERVER['HTTP_USER_AGENT']);

sumit270 at gmail dot com
12.06.2008 18:45
Prevent XXS attack

// Prevent any possible XSS attacks via $_GET.
foreach ($_GET as $check_url) {
    if ((
eregi("<[^>]*script*\"?[^>]*>", $check_url)) || (eregi("<[^>]*object*\"?[^>]*>", $check_url)) ||
eregi("<[^>]*iframe*\"?[^>]*>", $check_url)) || (eregi("<[^>]*applet*\"?[^>]*>", $check_url)) ||
eregi("<[^>]*meta*\"?[^>]*>", $check_url)) || (eregi("<[^>]*style*\"?[^>]*>", $check_url)) ||
eregi("<[^>]*form*\"?[^>]*>", $check_url)) || (eregi("\([^>]*\"?[^)]*\)", $check_url)) ||
eregi("\"", $check_url))) {
    die ();
Jeff Morris
30.05.2008 20:06
Email Address RegEx -- The Final Frontier?

Inspired by bobocop's stalwart effort. Cheers for that matey!

Contrary to most folks' expectation, a quoted @ character is permitted in the
local part of an email address. So strictly speaking bobocop's test result for
'' is ...inconclusive?

The RFC prohibits control characters in the address. So it's no coincidence
that most header-related exploits try to inject control characters into the
fields sent to the server. If we're validating client-side, we need to ensure
user input is restricted to the printable code set. And in the spirit of not
trusting anything inbound, we need to filter again server-side. It's handy
to have the same regex working at both ends.

My variant of bobocop's regex is listed below. Note the mask for the local
part matches any printable character *excluding the dot*. The dot is reserved
as a label separator. Bobocop's regex enforces that role while ensuring the
local part does not start or end with a dot.

Outside of the 7-bit ASCII and dot rules, the RFC says 'anything goes' in the
local part. Them's the breaks folks.

All we need to realise is that our endeavours are limited, and the nearest
we'll get to validating an email address is finding an MX record in DNS.
Whatever, don't go probing mail servers with test emails, you might get more
than you bargained for. That's sp@mmer territory, that is.
If you want to positively vet a mail server, consider running a check against Search for the checkdnsrr function page on this site and
read the comments for good info.

Anyhoo, here's the modded regex builder:

//the variables
$local            = '[\x20-\x2D\x2F-\x7E]';
$alnum            = 'a-z0-9';
$domain            = "([$alnum]([-$alnum]*[$alnum]+)?)";

//the array
$arr            = array();
$arr['start']    = '^';
$arr['local']    = "$local+(\.$local+)*";
$arr['at']        = '@';
$arr['domain']    = "($domain{1,63}\.)+";
$arr['tld']        = "[$alnum]{2,6}";
$arr['end']        = '$';

//the regex
$regex            = implode('',$arr);


$regex evaluates to:


(regex split into 2 lines due to line length limits)

Add virgules front and back for the javascript equivalent.

I'm running this in an AJAX app right now and it does what it says on the tin.

If you're uncomfortable with the character length limits on domain and tld
names, change them to taste.

ted devito
3.05.2008 1:29
RE: validate a url
based on "ian at hyperborea dot co dot uk" below...
original date: 10-Nov-2004 03:15

I added a test for http(s?) and ftp as well as a trailing slash on urls that don't specify a page.

now it allows...

$domain = "(http(s?):\/\/|ftp:\/\/)*([[:alpha:]][-[:alnum:]]*[[:alnum:]])
$dir = "(/[[:alpha:]][-[:alnum:]]*[[:alnum:]])*";
$trailingslash  = "(\/?)";
$page = "(/[[:alpha:]][-[:alnum:]]*\.[[:alpha:]]{3,5})?";
$getstring = "(\?([[:alnum:]][-_%[:alnum:]]*=[-_%[:alnum:]]+)
$pattern = "^".$domain.$dir.$trailingslash.$page.$getstring."$";
mbfreight atthe gmail place
3.01.2008 23:53
keran at kiwi-interactive dot com wrote (5ish years ago) 07-Mar-2003 08:21

$feedback = "Error: $email isn't a valid mail address!";
return $feedback;
-- and --
$feedback = "Error: $domain isn't a valid domain!";
return $feedback;

I've been crushed with patching up XSS and anytime you get user input, it's best to just not show it back to them if possible. The auditor loves throwing these at me: >"><script>alert(123)</script><"  in the url, in forms, everywhere. Some looking around and you can find and build an amazing testing string.

I have found that using htmlentities($user_input) isn't enough, either. There are a few tricks that can help like
        // from
    while($input != strip_tags($input)) {
            $input = strip_tags($input);

In my case, I'm starting off by testing for <[tag]> as well as keyword() and then do some preg_replace ing.
shwetank dot sharma86 at gmail dot com
7.12.2007 14:13
we define some notficatio for making the Expression for eregi(exp, string)

so first of all syntax
   [ ]  this brakit  used to define chracters
   eg  [a-z], [0-9]
  { } this brakit used ti define range
  eg  {1,3}
if you wanna to make a expresion which take which take maximum
three digit no

and if make exp for only three digit no than

ok if any problem mail me
c00lways at gmail dot com
11.03.2007 3:10

i love your expression,
and i've came out with a solutions which does not need to set the number of times {1,3} for the domain.
it can accept unlimited number of times, but @ least 1 time .com / .xxx

$exp = "^[a-z0-9]+[a-z0-9\?\.\+-_]*" .
m at tthew dot org dot uk
7.11.2006 23:47
This example checks for a valid IP address or CIDR notation address range. (Thanks Walo for just the start I needed.)

The reg exp is too long to post in the code. It is:
(substitute for EXPR)


function checkValidIp($cidr) {

// Checks for a valid IP address or optionally a cidr notation range
    // e.g. or

if(!eregi("EXPR", $cidr)) {
$return = FALSE;
   } else {
$return = TRUE;
    if (
$return == TRUE ) {

$parts = explode("/", $cidr);
$ip = $parts[0];
$netmask = $parts[1];
$octets = explode(".", $ip);

        foreach (
$octets AS $octet ) {
            if (
$octet > 255 ) {
$return = FALSE;

        if ( (
$netmask != "" ) && ( $netmask > 32 ) ) {
$return = FALSE;




11.09.2006 23:33
It's probably worth noting that eregi() (and most likely, the related variations) appears to have a 255-character limit with respect to the length of the input it will attempt to parse.

If you try to do something like

if (!eregi("^[a-zA-Z0-9]{0,256}$", $text)) { ...

eregi() will return FALSE, irrespective of whether or not the input matches the pattern.
markus dot sipila at no dot spam dot iki dot fi dot invalid
2.08.2006 23:29
One more comment about email validation and usability of validators.

The fact that RFC 2822 allows broader set of characters in email addresses than typically used makes things quite challenging usability wise.

A very common usability problem with email validators is that they do not accept all valid addresses (such as foo{bar}.baz! Almost as  common problem is that the validator only checks that the syntax is valid and passes addresses like without any warning. Even though is syntactically valid it might just as well be a typo of

I resolved this usability challenge by doing the validation in two phases. In the first phase the address is validated so that it can't include exotic characters like { or |. Most addresses pass this validation.

If they don't, they are validated with the other validator that allows all RFC-compliant addresses. In this case the validator shows a message that the address is syntactically valid but it recommends to double check it for typos.

An example without regexps:
if (eregi($normal, $email)) {
"The address $email is valid and looks normal.");

else if (
eregi($validButRare, $email)) {
"The address $email looks a bit strange but it is syntactically valid. You might want to check it for typos.");

else {
"The address $email is not valid.");

The full article with the regexps and demo can be found at
tim at rocketry dot org
30.04.2006 6:32
The easiest way I've found to validate a properly formed email address is this:

if(!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $_POST['EmailAddress'])) {
     echo "<p>Not a valid email address</p>\n";

It basically just wants to see some alphanumeric characters + an @ sign + a . + 2 to 4 alpha characters. So far it has done what I need for quite a while now.. Hope that helps someone. :)

info at stenschke dot com
3.02.2006 13:39
i needed a function to find hyperlinks containing a url as text of the hyperlink, exceeding a given maximum length.
here my function to finds too long hyperlinks and insert <br />s where needed into the linktext:

    function breakTooLongLinks($text,$maxLen) {
        //find hyperlinks that contain too many chars & insert <br>s where neccessary       
        $pattern= '[>]www[.].*/*(.doc|.pdf|.htm|.html|.shtml|.php|.asp)(</a>|</A>)';
        $match= eregi($pattern, $text, $regs);
        if ($match) {
            foreach ($regs as $link) {
                if (strlen($link)>$maxLen) {
                    $linkParts= explode('/',$link);
                    $linkRepl= array(); $replI=0; $curLinkPart='';
                    foreach($linkParts as $linkPart) {
                        $curLinkPart.= $linkPart.'/';
                        if (strlen($curLinkPart)>$maxLen) {
                            $linkRepl[]= $curLinkPart;
                    $linkRepl= implode('<br />',$linkRepl);                   
                    $text= str_replace($link, $linkRepl, $text);
        return $text;
opedroso at swoptimizer dot com
7.01.2006 14:13
I you are trying to match accented characters (e.g. you want to match "Caf Ol"), you can use the following regex:

"[a-z- ]+"

This takes advantage of the fact that in the iso-8859-1 "Latin 1", the "" is the first accented char and the "", the last one.

By the way, the regex above will match this string as a single match:

"aeiou "

Beware that your local code page may include a few more char codes in the range "[-]", so it may match more than you expect.
Jason Smart knarlin at yahoo dot com dot au
15.10.2005 7:28
There seems to be a lot of confusion about escaping special characters within a bracket expression enclosed in []

The reference is the regex man page at , Linked from the "Regular Expression Functions (POSIX Extended)" page in the PHP manual at

The simple rule is there should be no backslash `\' in a bracket expression unless you are matching it literally.
ALL special characters (the few exceptions are listed below), lose their special significance in a bracket expression.
This includes .[$()|*+?{\ all of which have special meaning outside of a bracket expression, inside they will match LITERALLY!

The backslash character can never escape anything in a bracket expression including itself! It is always interpreted literally.

Here are the exceptions

^ at the start of the bracket expression will match on all but the set of characters within the bracket expression

- (hyphen) Indicates range, so [a-z] will match a single character in the range a-z (case insensitive for eregi). 
  To match a literal `-' it must be at the start or end of the bracket expression or the end of a range, or enclosed in [. .] to be the start of a range.
  Both the above will match a single character from the set comprising the digits and hyphen `-'
  [!--] is the same as [!"#$%&'()*+,-] 
  i.e. it matches a single character from the range of characters from `!' to hyphen `-'
  [[.-.]-9] is the same as [-./0123456789] or [./0123456789-]
  and will match a single digit from the range of characters from hyphen `-' to digit 9 

] The closing bracket indicates the end of a bracket expression but not if it immediately follows the opening bracket `[' or `[^' in that case the closing bracket is matched literally.
  [][0-9] is the same as []0-9[]
  and matches a single character from the set of digits and a literal closing bracket `]' and opening bracket `]'
  [^][0-9] is the same as [^]0-9[]
  and matches a single character from the group of all characters except the set of digits and literal `]' or `['

Look at the regex man page for more on the above and the following special cases

  A collating element
  An equivalence class
  A character class
That's it for special cases! ALL other characters match literally within a bracket expression and can never be escaped with `\'

Here is a list of some of the postings here that have mistakenly tried to use special characters within a bracket expression.
28-Jan-2002 12:56, 09-Feb-2002 06:28, 24-Sep-2002 01:21, 08-Mar-2003 02:21, 11-Apr-2003 03:22 (uses `|' as if it was `or' it is not, it is a literal `|'), 27-Jun-2003 04:03, 06-Sep-2003 07:17, 20-Apr-2004 04:55, 24-Nov-2004 09:12, 25-Nov-2004 02:11, 06-Jan-2005 08:54, 24-Feb-2005 06:05

It seems people are copying each others mistakes so this needs to be cleared up. Most of the mistakes attempt to escape special characters within a bracket expression with backslash. That never works! A simple fix is to simply remove any backslashes within the bracket expression that are not meant to be matched literally.

The attempt to use | as 'or' within a bracketed expression should be put in parentheses instead.

I used a simple script attached to a form to test these results. Here's the script.
if(isset($_POST['pattern']) && isset($_POST['teststring'])) {
$pattern = stripslashes($_POST['pattern']);
$teststring = stripslashes($_POST['teststring']);
$match = ereg($pattern, $teststring, $regs);
$match) $match = 0;
'<p>The regular expression is: '.$pattern.'</p>');
'<p>The string for testing is: '.$teststring.'</p>');
'<p>The number of matching characters is: '.$match.'</p>');
Just use a form with input fields named "pattern" and "teststring"

Hope this helps and clears up some misconceptions
Jason Smart
Monash University Student
manuel at cvam dot com dot ar
28.07.2005 0:47
Please note that event though email pattern submitted by bobocop at bobocop dot cz below is RFC compliant, actual domain names are not!

They do start with numbers (as in, etc), so the second line, domain, should be:
= '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';
(everything else just like bobocop's comment from 02-May-2005 11:09)
somebody at somewhere dot com
10.05.2005 6:35
Say you want to validate a random sized string (at least one character though), and make sure it DOESN't contains anything BUT these characters: [:space:]a-zA-Z0-9_.-

function valid_name($name)
   // return FALSE if it contains characters which
   // which ARNT on the specified list
   if(ereg('[^[:space:]a-zA-Z0-9_.-]{1,}', $name))
      return false;
      return true;

bobocop at bobocop dot cz
2.05.2005 17:09
// Completely update for match RFC 2822 and RFC 1035

// Example results:

$email[] = '';                      // matched
$email[] = '';                // matched
$email[] = '';                  // matched
$email[] = '';                 // matched
$email[] = 'foo@example.example';                  // matched
$email[] = '%#a+f.*&654_-._@ee.xx';                // matched
$email[] = 'foo@abc-123.xx';                       // matched
$email[] = 'a@a.a.a.a.aa';                         // matched
$email[] = 'a@a9.aa';                              // matched
$email[] = 'a!b#c$d%e^f&g*h\'i+j-k{l|m}n_/@op.qr'; //matched

$email[] = '';                                     //separator

$email[] = '';                     // not matched
$email[] = '';                     // not matched
$email[] = '%#af.*&@a%#b.xx';                      // not matched
$email[] = 'a@a.99.00.a.aa';                       // not matched
$email[] = '_-._@-.--';                            // not matched
$email[] = 'any..thing@bla.bla';                   // not matched
$email[] = '@.';                                   // not matched
$email[] = '';                                // not matched
$email[] = '';                       // not matched
$email[] = ' @ .com';                              // not matched
$email[] = '';                     // not matched
$email[] = '';                     // not matched
$email[] = 'foo@example.x';                        // not matched

$atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]';    // allowed characters for part before "at" character
$domain = '([a-z]([-a-z0-9]*[a-z0-9]+)?)'; // allowed characters for part after "at" character

$regex = '^' . $atom . '+' .         // One or more atom characters.
'(\.' . $atom . '+)*'.               // Followed by zero or more dot separated sets of one or more atom characters.
'@'.                                 // Followed by an "at" character.
'(' . $domain . '{1,63}\.)+'.        // Followed by one or max 63 domain characters (dot separated).
$domain . '{2,63}'.                  // Must be followed by one set consisting a period of two
'$';                                 // or max 63 domain characters.

foreach ($email as $example) {
    if (
strlen($example) == 0):
      if (
eregi($regex, $example)):
$example . ' matched<br>';
'<strong>'. $example . ' not matched</strong><br>';
Sergio Santana: ssantana at tlaloc dot imta dot mx
2.03.2005 1:32
Sometimes we require checking the syntax of
floating point numbers. Here is a php-code that
performs this task.

// To call this program:
//   php floats.php
// Example of the use of regular expressions for checking
// the syntax of floating point numbers.
// This opens standard in ready for interactive input..
$STDIN = fopen("php://stdin","r");

for(;;) {
echo "Float>";
$s = trim(fgets($STDIN,256));
  if (
$s) {
$le =
"-?(([0-9]+e-?[0-9]+)|((([0-9]+\.[0-9]*)|(\.[0-9]+))" .
$s, $regs);
    if (
"Yes it's float\n";
    else if (
$le > 0)
"$le chars of the string correspond to a float\n";
"No, it isn't float\n";


krum at estnet dot bg
23.02.2005 21:05
I was searching for a fast way to check all fields of a form, so I can
include the $_POST information directly to the sql query. So I wrote
another script in my class in order to check the submitted data. So here it
function check_post () {
 foreach (
$_POST as $key => $val) {
$key = $val;
$data = Array();
$data["^[a-z0-9!@#$%^&*]"] = Array ("first_name", "last_name",
"nick", "pass", "description", "user", "message", "event", "name", "title",
.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$'] = Array("mail");
$data["^[0-9]{1,4}"] = Array("day", "month", "owner", "type", "forum_id", "topic_id");
 foreach (
$data as $key => $val) {
  foreach (
$val as $k => $c) {
   if ( isset($
$c) ) {
    if ( !
eregi($key, $$c) ) {
"You have used an not allowed character in " .$$c. " field\n";
I hope it will help someone ^_^
3.02.2005 20:49
The  gethostbyname($regs[2]) == $regs[2] is a bad idea.  Some domain names have a MX record but not an IP number.  The first example I found is domain:

sven@chef:~$ host A record currently not present
sven@chef:~$ host -t mx                 MX      10
selman17@yahoo com
24.11.2004 17:11


$ilet=explode(" ",$iletisim);

$ilet[$i]= str_replace("$ilet[$i]","<a href='mailto:$ilet[$i]'>$ilet[$i]</a><br>",$ilet[$i]);
$ilet[$i]= str_replace("$ilet[$i]","<a href='$ilet[$i]' target='_blank'>$ilet[$i]</a><br>",$ilet[$i]);
$iletisim .=$ilet[$i];
/* this will out put
<a href=""></a><br>
<a href="" target="_blank"></a><br>
<a href=""></a>
cristian81 AT katamail DOT com
24.11.2004 12:12
The same "checkipaddress" function posted by "lueck at gerwan dot de", with the add of an optional jolly char suport. Returns TRUE or FALSE


check_ip_address('') is TRUE
check_ip_address('125.32.15.*') is FALSE
check_ip_address('125.32.15.*', '*') is TRUE
check_ip_address('*.*.*.*', '*') is TRUE
check_ip_address('125.-.15.2', '-') is TRUE

function check_ip_address($checkip, $jolly_char='') {
    if (
$jolly_char=='.')        // dot ins't allowed as jolly char
$jolly_char = '';
    if (
$jolly_char!='') {
$checkip = str_replace($jolly_char, '*', $checkip);        // replace the jolly char with an asterisc
$my_reg_expr = "^[0-9\*]{1,3}\.[0-9\*]{1,3}\.[0-9\*]{1,3}\.[0-9\*]{1,3}$";
$jolly_char = '*';
$my_reg_expr = "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$";
    if (
eregi($my_reg_expr, $checkip)) {
        for (
$i = 1; $i <= 3; $i++) {
            if (!(
substr($checkip, 0, strpos($checkip, ".")) >= "0" && substr($checkip, 0, strpos($checkip, ".")) <= "255")) {
                if (
$jolly_char!='') {            // if exists, check for the jolly char
if (substr($checkip, 0, strpos($checkip, "."))!=$jolly_char)
$checkip = substr($checkip, strpos($checkip, ".") + 1);
        if (!(
$checkip >= "0" && $checkip <= "255")) {        // class D
if ($jolly_char!='') {            // if exists, check for the jolly char
if ($checkip!=$jolly_char)
ian at hyperborea dot co dot uk
10.11.2004 12:15
I was looking for something to validate a URL, and as usual turned to '' as it provides an invaluable source of information. I couldn't find exactly what I wanted so came up with something of my own which I hope will be of use to others.

function validateURL($URL) {
$domain = "([[:alpha:]][-[:alnum:]]*[[:alnum:]])
$dir = "(/[[:alpha:]][-[:alnum:]]*[[:alnum:]])*";
$page = "(/[[:alpha:]][-[:alnum:]]*\.[[:alpha:]]{3,5})?";
$getstring = "(\?([[:alnum:]][-_%[:alnum:]]*=[-_%[:alnum:]]+)
$pattern = "^".$domain.$dir.$page.$getstring."$";
eregi($pattern, $URL);
Be sure to enter each pattern on a single line.

The function accepts a URL and validates the domain, directory path, page and any GET parameters. You can see I have split the patterns for validating each section for easier reading and maintaining.

Domain Name:
This can be made of of many parts each seperated by a '.' (dot). each part must start with an alpha char. followed by any alphanumeric chars., or '-'. With the exception of the first part each part must end in an alpha char..

e.g. and are valid, but and are not.

Directory Path:
Each directory must start with a '/' followed by an alpha char., then any alpha numeric char., or '-', and ending in an alphanumeric char.

e.g. /path1/to-page is valid, but /path1-/2-page is not.

This is like the directory path but it must end with a '.' (dot) followed by 3-5 alpha chars.

e.g. /phpinfo.php and /page.shtml are valid, but /phpinfo.php4 and /pageshtml are not.

Get String:
This follows the page and begins with a '?'. This is then followed by key/value pairs each seperated by an '&'.

e.g. ?key1=value1&key2=value2

Each key, or value must start and end with an alpahnumeric char. I have allowed for a '%' to be specified for URL encoded chars., and also '_' (underscore) is allowed.

Well thats it hope it is of some use. It is NOT perfect, for instance in the 'getstring' I would like to ensure that encoded characters (i.e. %20) consist of a % symbol followed by two characters with values '0-9A-F' but couldn't get anything I tried to work.
I would welcome any suggestions or improvements, there are bound to be some.
Note: The patterns are based on an earlier post by 'fusillo at NOSPAM dot netgang dot it' for matching domain names. Thanks to him.
gabriel at gabrielharrison co uk
1.11.2004 13:19
There is a bug in the regex that means it doesn't work with a domain suffex that has two parts eg:
I'm not good enough with regexs to sort this - sorry.

There can also be a problem if the domain doesn't have a domain entry without the www. subdomain. To solve this modify the code with:

elseif( gethostbyname($regs[2]) == $regs[2] ||
gethostbyname('www' . $regs[2]) == 'www' . $regs[2])
chadkeffer at mail dot com
1.07.2004 13:42
There's seems to be some confusion on just what is allowed in an email address and thus how to build your regular expression.  RFC 2822 replaced RFC 822 over 3 years ago, so make sure you're referencing the correct RFC.  Also, don't assume all servers comply with the RFC.
gullevek at gullevek dot org
23.06.2004 10:58
for the comment from 'joseph dot ragsdale at xconduit dot com'

The regex has one bug thought. It elminates <user> addresses which are completly valid as it needs at least 2 chars in the domain part.

This regex fixes it:


[but the regex in one line]
kidproto at hotmail dot com
20.04.2004 8:55

That is the proper file check for a well formatted beginning and ending filename for those select files. The one in this list is incorrect for at least php 4.3.0 which is what I'm using.

The (:?) makes sure that the parentheses aren't used for storing the matched regex.
fusillo at NOSPAM dot netgang dot it
3.01.2004 23:24
This is a simple function that uses eregi() function to validate a domain name (according to the RFC 1034).

function check_host($host) {
if (
  ) {
       return 1;
    } else return 0;

NOTE: This expression must be written on a single line to work
I had to brake it on separate lines to post on the site.

Returns 1 if $host is an RFC compliant domain name else returns 0.

This function is part of the code of blackboxed linux distribution coming on your screen late genuary.

11.12.2003 9:30
A little interesting and hopefully on topic point I found while browsing the internet at:

According to RFC 822 (see, email addresses
are case-sensitive in the part before the "@" sign, except for the
special address "POSTMASTER", which is not case-sensitive. The part
after the "@" sign is a host name, and is not case-sensitive.

Note that many organizations will implement an aliasing facility that
recognizes alternative forms for an email address (e.g., providing
"", "",
"", "" etc.). However,
this is not required by the standard as specified in RFC 822.

so you may want to keep that in mind....
i.e. breakup the username/alias (ereg) and domain (eregi) parts of the email
joseph dot ragsdale at xconduit dot com
6.11.2003 23:31
I'm posting this as a - hopefully - helpful example to those who are learning regular expressions. Apologies to the documentation team and moderators for posting yet another email address validation example, but it's hard to argue against it as most everyone will see regular expressions as being a solution to this problem.

// Example results:

$email[] = '';       // matched
$email[] = ''; // matched
$email[] = '';   // matched
$email[] = ''// matched
$email[] = 'foo@example.example';   // matched
$email[] = '_-._@-.--';             // matched

$email[] = '@.';               // not matched
$email[] = '';            // not matched
$email[] = ' @ .com';          // not matched
$email[] = ''; // not matched
$email[] = ''; // not matched
$email[] = 'foo@example.x.y'// not matched

$regex =
'[_a-z0-9-]+'.        /* One or more underscore, alphanumeric,
                           or hyphen charactures. */
'(\.[_a-z0-9-]+)*'.   /* Followed by zero or more sets consisting
                           of a period and one or more underscore,
                           alphanumeric, or hyphen charactures. */
'@'.                  /* Followed by an "at" characture. */
'[a-z0-9-]+'.         /* Followed by one or more alphanumeric
                           or hyphen charactures. */
'(\.[a-z0-9-]{2,})+'. /* Followed by one or more sets consisting
                           of a period and two or more alphanumeric
                           or hyphen charactures. */

foreach (
$email as $example) {
  if (
eregi($regex, $example)) {
$example . ' matched<br>';
  } else {
$example . ' not matched<br>';
banerian at u dot washington dot edu
17.09.2003 3:55
It should be noted that in the function validateEmail, the final verification relies on the mail server responding to the test $email with a reply code of 250.   Nowadays, many/most/all servers will reply 250/Ok for any  user@thier.domain regardless of whether or not the userid actually exists.  mail servers tend to accept the mail (if it passes other checks), and if the user really does not exist, it just "bounces" the mail back to sender.  thus the validateEmail function can give false unfortunate victim of having to deal with spam.
6.09.2003 11:17
Most of the Ereg functions for emails that I have seen do not
work for emails like :-

And emails such as

This eregi which we put together, I think works, it should be just one line, but did not let us submit, because it said that even after using wordwrap() the line was too long :-

$ereg_string =

Adjust the number "3" right at the end to whatever number
you want.

A long domain name such as "" which
has 5 parts will require the number to be higher.
tomi at vacilando dot net
26.06.2003 20:03
Keran's solution is a good one but I found it does not work for a small number of domains -- because of a bug in gethostbyname() that causes some valid domains not to resolve properly. The workaround is to test such domains with "www." added to the beginning. This script does all that and works perfectly for me. Enjoy!

// Function to validate emails.
function validate_email($email_raw)
    // replace any ' ' and \n in the email
    $email_nr = eregi_replace("\n", "", $email_raw);
    $email = eregi_replace(" +", "", $email_nr);
    $email = strtolower( $email );
    // do the eregi to look for bad characters
    if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*". "@([a-z0-9]+([\.-][a-z0-9]+))*$",$email) ){
      // okay not a good email
      $feedback = 'Error: "' . $email . '" is not a valid e-mail address!';
      return $feedback;
      } else {
      // okay now check the domain
      // split the email at the @ and check what's left
      $item = explode("@", $email);
      $domain = $item["1"];
      if ( ( gethostbyname($domain) == $domain ) )
          if ( gethostbyname("www." . $domain) == "www." . $domain )
               $feedback = 'Error: "' . $domain . '" is most probably not a valid domain!';
               return $feedback;
        // ?
           $feedback = "valid";
           return $feedback;
        } else {
        $feedback = "valid";
        return $feedback;
steve at brainwashstudios dot com
10.04.2003 19:22
why not match some filenames?

if (!ereg("^[a-ZA-Z0-9]+[/.gif|/.jpg|/.png]$",$match){
 echo "Invalid Filename";
 if (file_exists($match)){
  echo "blah";

I suppose that would work off the top of my head but I have yet to test it, so you may want to check it first.
keran at kiwi-interactive dot com
8.03.2003 5:21
I couldn't get any of the email validation items above to actually work (maybe I'm thick) :)

So I adapted a couple and came up with this, and it seems to work

// function to validate email
function validate_email($email_raw) {

    // replace any ' ' and \n in the email
    $email_nr = eregi_replace("\n", "", $email_raw);
    $email = eregi_replace(" +", "", $email_nr);
    // do the ergei to look for bad characters
  if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*".
"@([a-z0-9]+([\.-][a-z0-9]+))*$",$email) ){
    // okay not a good email
    $feedback = "Error: $email isn't a valid mail address!";
    return $feedback;
    } else {
        // okay now check the domain
        // split the email at the @ and check what's left
        $item = explode("@", $email);
        $domain = $item["1"];
        if( gethostbyname($domain) == $domain) {
            $feedback = "Error: $domain isn't a valid domain!";
            return $feedback;
        } else {
            $feedback = "valid";
            return $feedback;

slavo at polovnictvo-mn dot sk
20.02.2003 2:30
This is what I found (by Jon S. Stevens with
Copyright 1998 Jon S. Stevens, Clear Ink)

function validateEmail ($email){
   global $SERVER_NAME;
   $return = array(false,  "" );
   list ($user, $domain) = split( "@", $email, 2);
   $arr = explode( ".", $domain);
   $count = count ($arr);
   $tld = $arr[$count - 2] .  "." . $arr[$count - 1];
   if(checkdnsrr($tld,  "MX")) {
      if(getmxrr($tld, $mxhosts, $weight)) {
         for($i = 0; $i < count($mxhosts); $i++){
               $fp = fsockopen($mxhosts[$i], 25);
            if ($fp){
               $s = 0;
               $c = 0;
               $out =  "";
               set_socket_blocking($fp, false);
               do {
                  $out = fgets($fp, 2500);
                  if(ereg( "^220", $out)){
                           $s = 0;
                     $out =  "";
                  else if(($c > 0) && ($out ==  "")){
                  else {
                  if($s == 9999) {
               } while($out ==  "");
               set_socket_blocking($fp, true);
               fputs($fp,  "HELO $SERVER_NAME\n");
               $output = fgets ($fp, 2000);
               fputs($fp,  "MAIL FROM: <info@" . $tld .  ">\n" );
               $output = fgets($fp, 2000);
               fputs($fp,  "RCPT TO: <$email>\n");
               $output = fgets($fp, 2000);
               if(ereg(  "^250", $output )) {
                  $return[0] = true;
               else {
                  $return[0] = false;
                  $return[1] = $output;
               fputs ($fp,  "QUIT\n");
               if($return[0] == true){
   return $return;

hope it helps you...
webmaster at textedit dot co dot uk
19.01.2003 20:03
I notice a lot of queries involving regular expressions and is_float() regarding currency validation.

If you have a version of php that uses the std C ctype.h functions then it will ALWAYS work with the following code.

function check_price($price)

    if ( count($data) != 2 )
        return "false";

    if ( ctype_digit($data[0]) && ctype_digit($data[1]) && $data[0][0] != '0')
        return "true";
          return "false";

Peter Lorimer
18.01.2003 3:48
To check email, i use the following code:

if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $email) {
echo "Invalid Email Adress";
else {
echo "Valid Email Adress";
respectthepinguin at yahoo dot com
4.01.2003 8:30
validating an email can be pretty tough... however this....
makes this task much easier. (:)
BKDotCom at hotmail dot com
30.09.2002 20:56
in regards to using gethostbyname() to validate email addresses:
Large flaw:
Doesn't work if the host has a NS & MX record, but no A record.
mark dot edwards dot no dot spam at rayconNOSPAM dot com dot au
24.09.2002 5:21
The apostrophe is an allowable character in the username of an email address.

The regex I use for this is:

Escaping the apostrophe unfortunately allows users to put a backslash in their username and the regex will not complain.

Better I feel to allow some errors through than to deny legitimate addresses.
henrik jensen
27.07.2002 19:05
Looking for at regex to check if a file is an image file, this seems to work?

Note: it does not check for illegeal filesystem names - it only looks at the filename extension.

if (eregi ("(.)+\\.(jp(e){0,1}g$|gif$|png$)",$filename)){
    // This is an imagefile

[ remove this_ from emailaddress ]
trosho at vip dot bg
9.06.2002 13:47
Please note that eregi() is case sensitive if you use cyrillic chars
re at lloc dot de
6.03.2002 16:36
An additional note to one of these
expressions above: I use

tuxx at tuxx-home dot at
27.02.2002 12:11
A small note to one of these expressions above:
Inside a character class ([...]) one does not need to escape the fullstop, therefore


would become

steve at fish2find dot co dot uk
26.02.2002 16:40
hope this helps with some validation problems, simple text validation:           
            //$firstname = "somename"; // valid
    or  //$firstname = "somenam3"; // not valid


            //lets validate a field entry & trim off any white spaces
            $firstname = trim($firstname);
        //set lenght of the field to a max 12 characters
            $len = "0,12";
            $field = $firstname;
            //call function
            if (is_valid($field, $len)){
            //if field entry valid then set
            $valid_firstname = $field;
            //set record to yes
            $valid_record = "TRUE";
                  echo "$field is a valid name";
            //set record to null
            $valid_record = "FALSE";
                  echo " $field is not a valid name";
            //process to see if valid record
            if ($valid_record == "FALSE"){
                  echo " & is not a valid record";
            else{ //assume record is valid
                  echo " & is a valid record";
            //validate field entry function
            function is_valid($field, $len) {
            if(eregi("^[[:alpha:]]{{$len}}$", $field)) return                TRUE;
            else return FALSE;
ruben dot no dot spam at artek dot no dot spam dot es
8.02.2002 21:28
Well, this can be improved a little. According to the previous, these email addresses would be correct:

I suggest this regexp:

if( !eregi( "^" .
            "[a-z0-9]+([_\\.-][a-z0-9]+)*" .    //user
            "@" .
            "([a-z0-9]+([\.-][a-z0-9]+)*)+" .   //domain
            "\\.[a-z]{2,}" .                    //sld, tld
            "$", $email, $regs)
mmc at nospam dot dk
27.01.2002 15:56
I'm not sure about "\." being the same as "." above.

Anyway, the mentioned regex would not recognize .museum-names, and generally isn't future safe.
Also, it doesn't verify that usernames and hostnames cannot start with "-._".

I would recommend a more general (=future safe) expression and then instead check the hostname. An example:

if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"
    $mail, $regs) )
    echo "Error: '$mail' isn't a valid mail address!\n";
elseif( gethostbyname($regs[2]) == $regs[2] )
    echo "Error: Can't find the host '$regs[2]'!<br>\n";

Note: I had to split the regex for it to fit this note.
Also note: The reason I'm using gethostbyname() and not getmxrr() or such is that getmxrr() doesn't work on Win2000/XP.

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)