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!

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 

 

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.

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.