Re: ereg
| From: | Jason k Larson | Date: | Fri, 18 Apr 2003 07:02:47 +0000 |
| Subject: | Re: ereg | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-144381@lists.php.net to get a copy of this message | ||
First of all you have too many \s in your regex match. That is the only reason the \ is being allowed. Secondly, you are simply doing a test, a regex comparison, you never modify the string. If you are trying to dump this into a database, namely mySQL, you are going to have to escape ' single-quotes, with single quotes. The slashes (which are a result of magic quotes) can be stripped if with strip slashes. All as demonstrated here:
// remove excess slashes
$formdata['lastname'] = stripslashes ($formdata['lastname']);
// sets $error if lastname contains other than alpha or singlequote
if (!ereg('^[a-zA-Z[:space:]\']*$', $formdata['lastname']))
{
$error = 'Invalid Input';
}
Hope that helps.
Note: I'd prefer to not mix and match, and to stay consistant using:
if (!ereg('^[[:alpha:][:space:]\']*$', $formdata['lastname'])) { ... }
--
der Ritter
aka: Jason k Larson
C.R. wrote:
This is what I have: if(!ereg("^[a-zA-Z[:space:]\\']*$", $formdata["lastname"])) {$error = "Invalid Input";} $formdata["lastname"] is a session variable and can't be changed as it is used throughout the site. I don't understand though why it it even being changed as I thought ereg was just an expression. Is there a better way to do this? TIA C.R.