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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.