Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

ereg

(PHP 3, PHP 4, PHP 5)

ereg -- Regular expression match

Description

int ereg ( string pattern, string string [, array &regs] )

Замечание: 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.

Замечание: 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:

Пример 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().



eregi_replace> <ereg_replace
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
ereg
mixailo at mercenaries dot ru
28-Jul-2007 04:31
preg_match is much more faster then ereg, MUCH MORE faster.
notbald at gmail spam away com
11-Jul-2007 01:26
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";
?>
ben at agaricdesign dot com
09-Jun-2007 03:49
Here is a tutorial on regular expressions in PHP, which I needed to create ereg functions not covered in the snippets:

http://www.zend.com/zend/spotlight/code-gallery-wade5.php
jaik at fluidcreativity dot co dot uk
31-Aug-2006 08:41
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;
}
?>
theppg_001 at hotmail dot com
19-Aug-2006 07:00
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);
?>
tomas at phusis dot co dot uk
06-Jun-2006 08:41
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;
}
?>
'morgan'.'galpin'.chr(64).'gmail'.'.com'
11-May-2006 01:16
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>
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571