Jump to content

Verifying form data - replacement for ereg()?


RLJ

Recommended Posts

Hi, I currently have some PHP scripts that verify form data using ereg() as follows:

 

if (ereg(".",$var1)==0)

{

//do something

}

 

where instead of the argument "." I also use "\@" or "\....*", etc. to test whether certain form input is in the correct format (e.g. testing for a valid email address, etc.).

 

However, I have understood that ereg() is being deprecated. What is the best/easiest function to replace ereg() with?

 

Thanks!

Link to comment
Share on other sites

preg_match  Would be the pcre equivalent.  The patterns will pretty much be the same, only real difference is that the preg_xxx functions require an opening/closing delimiter in the pattern and modifiers (if needed) are specified after the closing delimiter.

 

So for instance:

 

ereg(".",$var1)

 

would be

 

preg_match("~.~",$var1)  // ~ is used as the pattern delimiter.

 

 

or for instance,

 

eregi(".",$var1) // case in-sensitive 

 

would be

 

preg_match("~.~i",$var1)  // i modifier added to make it case in-sensitive 

 

Link to comment
Share on other sites

Cheers!

 

Just one more question: how do I use preg_match to write a very simple line of code to check whether a price is in the correct format?

 

All I want to do is allow only numbers "0-9" and a fullstop "." and make sure the entered price has between 1 and 5 digits. Also, there has to be at least one number before the "." and I want to convert e.g. 89 to 89.00 and 5.5 to 5.50 etc. o and also I want to replace "," with "." before checking so that someone can enter a price in european format: e.g. 5,50

 

Hopefully that is clear. I can do all of those things separately, but have been struggling to combine them.

 

Thanks for your help.

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.