preg_match_all problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingdm
Forum Commoner
Posts: 27
Joined: Thu Dec 03, 2009 9:32 am

preg_match_all problem

Post by kingdm »

Hello everyone,

I have this problem regarding a form validation on my address field. I want my address to contain no special characters, except comma(,) and period(.).

I did the two, but on the script the problem is even the spaces is considered illegal. How can I make the space valid?

Here is my preg code:

Code: Select all

if(preg_match_all('/[^a-z0-9_~\.,$]/i', $city_add, $invalid)) 
    { 
       $errors2[] = "Your address contains invalid character: " . join('',array_unique($invalid[0]));
    }
Thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: preg_match_all problem

Post by AbraCadaver »

Well, I don't know what you mean by special characters because you are allowing ~ and $, but put a space in your pattern!

Code: Select all

preg_match_all('/[^a-z0-9_~\.,$ ]/i', $city_add, $invalid)
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply