Jump to content

Can someone explain ereg() to me?


pahunrepublic

Recommended Posts

Hi Everyone!

 

There is this code:

eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email);

with eregi()function. It works fine but I don't understand  :confused: what does '^' , '$' , '\.' 'symbols mean in the function. What do they do?

 

Other thing:

I've heard that ereg() function will be deprecated in PHP6. Any alternatives?? I've heard preg_match() is the answer because it works the same way but I don't know how to use it. For example this line:

if(!eregi("^[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,10}$",$phone))

How would you code it down in preg_match()

 

Thanx the answers in advance folks!!

Link to comment
https://forums.phpfreaks.com/topic/205749-can-someone-explain-ereg-to-me/
Share on other sites

 

There are some pretty good regex tutorials (which I rely on, since I'm horrible with regex) that come up in a google search. As far as deprecation; from the PHP manual:

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

Ereg is depreciated. It is better to use preg_match which would be the equivalent.

 

As for what the special characters are, well see my signature for links to resources on Regular Expressions.

 

if (!preg_match('~^[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,10}$~', $phone)) {

 

Would be the "equivalent" to what you are doing. 

 

The ~ are delimiters, it does not have to be ~ it could be # / etc depending on your taste. The $ denotes the end of a string and the ^ denotes the start (when not used inside of parans). So the start of the string has to start with 3 numbers 0-9 and the end has to end with 1-10 numbers 0-9 or else it is not matched.

Ereg is depreciated. It is better to use preg_match which would be the equivalent.

 

As for what the special characters are, well see my signature for links to resources on Regular Expressions.

 

if (!preg_match('~^[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,10}$~', $phone)) {

 

Would be the "equivalent" to what you are doing. 

 

The ~ are delimiters, it does not have to be ~ it could be # / etc depending on your taste. The $ denotes the end of a string and the ^ denotes the start (when not used inside of parans). So the start of the string has to start with 3 numbers 0-9 and the end has to end with 1-10 numbers 0-9 or else it is not matched.

Thanx

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.