Jump to content

[SOLVED] checking if a string contains unwanted characters


wrathican

Recommended Posts

<?php
$allowed_chars = array('a',
                                                'b',
                                                'c',
                                                'd',
                                                'e',
                                                'f',
                                                'g',
                                                'h',
                                                'i',
                                                'j',
                                                'k',
                                                'l',
                                                'm',
                                                'o',
                                                'p',
                                                'q',
                                                'r',
                                                's',
                                                't',
                                                'u',
                                                'v',
                                                'w',
                                                'x',
                                                'y',
                                                'z',
                                                '1',
                                                '2',
                                                '3',
                                                '4',
                                                '5',
                                                '6',
                                                '7',
                                                '8',
                                                '9',
                                                '0',
                                                '!',
                                                '@',
                                                '$',
                                                '.',
                                                '+');
$word1 = str_split($_POST['word1']);

$is_invalid = array();
                foreach ($word1 as $value){
                        if (!in_array($value, $allowed_chars)){
                                $invalid_check['0'] = 1;
                        }

                }
if ($is_invalid['0'] = 1){
//boot them or something
}
//so on and so forth
?>

hey thanks for the reply, but im looking for something a little simpler.

 

i tried my hand at this and made this:

<?php

$string = "Hello!??!?";

$badchars = array(
				'!',
				'@',
				'£',
				'$',
				'%',
				'^',
				'&',
				'*',
				'(',
				')',
				'_',
				'+',
				'=',
				'-',
				']',
				'[',
				'\\',
				';',
				'/',
				'.',
				',',
				'`',
				'~',
				'<',
				'>',
				'?',
				':',
				'"',
				'|',
				'{',
				'}',
				' ',
			);


if (strpos($string, $badchars) === true) {
	echo "Invalid string";
}

?>

 

 

in theory it looks like this should work. but it doesn't pick it up.

This version tested to work.

<?php

$string = "Hello!??!?";
$string_array = str_split($string);
$badchars = array('!','@','£','$','%','^','&','*','(',')','_','+','=','-',']','[','\\',';','/','.',',','`','~','<','>','?',':','"','|','{','}',' ');

foreach ($string_array as $value){
if (in_array($value, $badchars) == true) {
	echo "Invalid string";
	exit();
}
}

?>

Yes, regular expression might be eaiser for someone that knows them, but strpbrk() would be even easier and would be faster too.

 

http://us.php.net/manual/en/function.strpbrk.php

 

<?php
$string = "Hello!??!?";
$badchars = '!@£$%^&*()_+=-][\;/.,`~<>?:"|{} \'';
if (strpbrk($string, $badchars) !== false)
{
echo "Invalid string";
exit();
}



?>

That's a new function for me and it would no doubt be quicker, since it doesn't use regex.

 

On the otherhand, i think it's safer to have a whitelist of characters than a blacklist - it would be very easy to forget some of the characters you wanted to disallow.

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.