Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 65
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Na początku, kiedy zobaczyłem, że ktoś chce jakiejś opłaty za pomoc w tworzeniu stron ryknąłem śmiechem - potem przyszły problemy... i zaryzykowałem. Druga rzecz to: nie chciałem "kopiować". Ale prawda jest taka: są lepsi, bardziej doświadczeni i... czasem trzeba poprosić o pomoc, a jak poświęca się na to trzecią cześć życia, to nic dziwnego, że nie chce się swoich "sekretów" zdradzać za darmo. Skorzystałem z "algorytmy.pl" i naprawdę jestem z tego w 100% zadowolony, polecam - dla zawodowców (co się uczą) i amatorów (można skorzystać z gotowego rozwiązania).

Tomasz Czypicki
Cybernoxa

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

ereg

(PHP 3, PHP 4, PHP 5)

ereg -- Regular expression match

Description

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

Notatka: 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.

Notatka: 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:

Przykład 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

ar_cat at shaw dot ca
15-Dec-2005 12:13

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 :)


net_navard at yahoo dot com
15-Nov-2005 04:35

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


bnewbold at codegreene dot com
03-Nov-2005 11:36

mcallier at gmail dot com's expression for zip codes is still wrong.
"^[0-9]{5}(-[0-9]{4})*$" will call the following zip codes good:

91425
91425-3444
91425-3444-3455
91425-3444-3455-4556
(etc)

the star (*) in the expression means zero or more. 
Instead it should be a question mark (?) meaning zero or one only.

use "^[0-9]{5}(-[0-9]{4})?$"


Jason Smart knarlin at yahoo dot com dot au
16-Oct-2005 10:13

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); ?>


mcallier at gmail dot com
04-Oct-2005 10:21

I think that fox's zip code check may need a slight modification. Here is fox's code:

$error_code=(ereg("^[0-9]{5}(-[0-9]{4})$",$zip))? NULL:1;

This will error on valid zipcodes such as 99212. If you change the regular expression to:

$error_code=(ereg("^[0-9]{5}(-[0-9]{4})*$",$zip))? NULL:1;

It will not error on valid zipcodes such as 99212.

To test this you may use the following code:

<?php
$testzip
[]="99212";
$testzip[]="9921";
$testzip[]="99212-1234";
$testzip[]="9921a";
$testzip[]="99212-12";
$regex['fox']="^[0-9]{5}(-[0-9]{4})$";
$regex['mcallier']="^[0-9]{5}(-[0-9]{4})*$";
foreach(
$regex as $key=>$expression)
{
   echo(
$key."<br>\n");
   echo(
"-----------<br>\n");
   foreach(
$testzip as $value)
   {
      
$test=ereg($expression,$value);
      
$test==1?$test="good":$test="bad";
         echo(
"$value=$test<br>\n");
   }
   echo(
"<br>\n");
}
?>

The output of this is:

fox
-----------
99212=bad
9921=bad
99212-1234=good
9921a=bad
99212-12=bad

mcallier
-----------
99212=good
9921=bad
99212-1234=good
9921a=bad
99212-12=bad


Jason knarlin at yahoo dot com dot au
02-Oct-2005 08:19

A previous post stated "Beware that eregs in PHP4 are broken, for instance if you want to match "a to z, 0,9 and -" you might want to use [a-z0\-9] but it will silently fail due to a bug"

This is incorrect, this is not a bug, nor is ereg broken on this count!
In my testing of ereg, matching `-' `[' or `]' within a bracketed expression ereg works exactly according to the regex man page http://www.tin.org/bin/man.cgi?section=7&topic=regex referred from the php manual page at http://au3.php.net/manual/en/ref.regex.php

From the regex man page you will find that within a bracketed expression to match a literal `]'  make it the first character (following a possible `^'). To include a literal `-' make it the first or last  character, or the second endpoint of a range. To use a literal `-' as the first endpoint of a range, enclose it in `[.'  and  `.]'

spook says
if you want a string to work that matches "a to z", "A to Z", "0 to 9", "-", "[", "]" and "_" you will have to use this expression:
([0-9a-zA-Z_]|\-|\[|\])
end spook says

A neater solution is
[]0-9a-zA-Z_[-]

I hope this settles that ereg is not buggy or broken in this regard

(I reposted as the preview on this board misled me, I thought I had to escape \ )


Jason knarlin at yahoo dot com dot au
02-Oct-2005 06:48

A previous post stated "Beware that eregs in PHP4 are broken, for instance if you want to match "a to z, 0,9 and -" you might want to use [a-z0\\-9] but it will silently fail due to a bug"

This is incorrect, this is not a bug, nor is ereg broken on this count!
In my testing of ereg, matching `-' `[' or `]' within a bracketed expression ereg works exactly according to the regex man page http://www.tin.org/bin/man.cgi?section=7&topic=regex referred from the php manual page at http://au3.php.net/manual/en/ref.regex.php

From the regex man page you will find that within a bracketed expression to match a literal `]'  make it the first character (following a possible `^'). To include a literal `-' make it the first or last  character, or the second endpoint of a range. To use a literal `-' as the first endpoint of a range, enclose it in `[.'  and  `.]'

spook says
if you want a string to work that matches "a to z", "A to Z", "0 to 9", "-", "[", "]" and "_" you will have to use this expression:
([0-9a-zA-Z_]|\\-|\\[|\\])
end spook says

A neater solution is
[]0-9a-zA-Z_[-]

I hope this settles that ereg is not buggy or broken in this way


eregitate
09-Sep-2005 03:01

It was really hard for me to get the next checker!!!
I think the information displayed here is really scarce.
I had to get a PHP book to get the following information:

to indicate the start of a string : ^, nothing comes before it

you'll need to escape the following characters (with a backslash \ ) :
. \ ! + * ? [ ] ^ $ ( ) = ! < > | and :
the characters above each have a special meaning,
that's why you need to escape them to include them in a string.

* means : match this pattern 0 or more times
? means : match this pattern exactly 1 time
+ means : match this pattern 1 or more times

and $ means the definite end of a string, nothing may come afterward.

With this information I could finaly check a complete string containing only a few allowed character types :

ereg("^([-a-zA-Z0-9_\.\!@#\$&\*\+\=\|])*$" , $var)

some of the other tips here let other character through like < and >,
which I did not want to get past my check! Hope this helps out.


chrismax2u at yahoo dot com dot cn
02-Sep-2005 10:15

use ereg() and checkdate()

$input_date="2005-09-02";
//check date with ereg()
if (!ereg("^(20[0-9]{2})-(0[1-9]|1[0-2])-([12][0-9]|3[01])$",
$input_date))
{
   echo "<script>alert(\"pass!\")</script>";
}

//check date with checkdate()
$explode_date=explode("-",$input_date);
if (!checkdate($explode_date['1'],$explode_date['2'],
$explode_date['0']))
{
   echo "<script>alert(\"stop!\")</script>";
   exit;
}


fox
10-Aug-2005 05:13

concerning the 'valid postal code' bit from 25 july -

regular expressions are good for turning the longer zip checking code given below into much shorter snippets, such as the following (which accomplishes the same zip code check):

$error_code=(ereg("^[0-9]{5}(-[0-9]{4})$",$zip))? NULL:1;

the expression requires the first five digits and allows an additional four; the hyphen is required if the optional four are present


puremango dot co dot uk at gmail dot com
19-Jul-2005 03:34

for constructing regexes, I recommend

http://www.weitz.de/regex-coach/

-it highlights the match as you type it!!!


Joel Weierman
22-Jun-2005 09:56

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)


irlkersten at gmail dot com
22-Jun-2005 08:54

On a small note to email checking:
Recently it is possible to register domains like www.k

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt