Re: How can I check for characters in a $_POST[] variable?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Or...  just use:

if(preg_match('/^[A-Za-z0-9]+$/', $_POST['username']) !== 0) {
// string contains other characters, write the code
}

You can see this regex in action here: http://regexpal.com/?flags=&regex=
^%5BA-Za-z0-9%5D%2B%24&input=myusername01

If you put anything different of A-Za-z0-9 the regex will not match.

Regards,
Igor Escobar
*Software Engineer
*
+ http://blog.igorescobar.com
+ http://www.igorescobar.com
+ @igorescobar <http://www.twitter.com/igorescobar>





On Thu, Sep 22, 2011 at 10:03 AM, Igor Escobar <titiolinkin@xxxxxxxxx>wrote:

> Use this regex:
> if(preg_match('/[[:punct:]]/', $_POST['username']) !== 0) {
>
> // string contains other characters, write the code
> }
>
> The POSIX class [:punct:] means matches any punctuation and symbols in
> your string and that includes [!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]
>
>
> Regards,
> Igor Escobar
> *Software Engineer
> *
> + http://blog.igorescobar.com
> + http://www.igorescobar.com
> + @igorescobar <http://www.twitter.com/igorescobar>
>
>
>
>
>
>
> On Thu, Sep 22, 2011 at 9:17 AM, Nilesh Govindarajan <contact@xxxxxxxxxxxx
> > wrote:
>
>> On Thu 22 Sep 2011 08:25:29 PM IST, Eric wrote:
>> > I have this problem when using php because my computer recognizes
>> > the characters "." and ".." as an existing file when I use file_exists.
>> Also
>> > I want to check $_POST["username"] for characters other then A-Z a-z and
>> 0-9.
>> > If it contains anything other then, I would like to prompt the user but
>> > I can't seam to use foreach properly and I don't know how to itterate
>> > through the post variable with a for loop while loop or do while loop.
>>
>> file_exists() for . and .. would always return true, because they
>> really exist! . is an alias for the current directory and .. for the
>> parent directory. This is irrespective of OS.
>>
>> To search $_POST["username"] for characters other than A-Z, a-z, 0-9,
>> you can use preg_match something like this (there's an alpha class as
>> well, but I'm not sure about it):
>>
>> if(preg_match('(.*)^[A-Za-z0-9]+', $_POST['username']) !== 0) {
>> // string contains other characters, write the code
>> }
>>
>> --
>> Nilesh Govindarajan
>> http://nileshgr.com
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux