Connecting Tech Pros Worldwide Forums | Help | Site Map

how to test string whether it contains special characters

Familiar Sight
 
Join Date: May 2007
Posts: 137
#1: Mar 18 '08
how to test string whether it contains special characters
--------------------------------------------------------------------------------

please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid ex for special characters (",',#,!,>,<,?) except any letters for other languages ?

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#2: Mar 18 '08

re: how to test string whether it contains special characters


Try preg_match()

[php]
/*
- Using preg_match() we'll check for illegal characters:
*/
$_string = "lolzz#";
if(preg_match('/[^a-zA-Z0-9_-]+/', $_string))
# checks for anything BUT: a through z (upper and lower case) 0 - 9, underscore _ and hypen -
{
echo "String contained illegal characters";
}
else
{
echo "String was legal.";
}
[/php]
If you don't understand, then check out the php.net website.

Regards
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#3: Mar 18 '08

re: how to test string whether it contains special characters


Quote:

Originally Posted by smartic

how to test string whether it contains special characters
--------------------------------------------------------------------------------

please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid ex for special characters (",',#,!,>,<,?) except any letters for other languages ?

Just my curiosity: by 'foreign letters' do you mean Unicode (UTF-8) characters or just <255 characters?

Ronald
Familiar Sight
 
Join Date: May 2007
Posts: 137
#4: Mar 18 '08

re: how to test string whether it contains special characters


if i use preg_match or ereg to catch special characters it deal with other languages like arabic as sepecial characters how can i deal with that?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#5: Mar 18 '08

re: how to test string whether it contains special characters


Quote:

Originally Posted by smartic

if i use preg_match or ereg to catch special characters it deal with other languages like arabic as sepecial characters how can i deal with that?

Now you are talking about Unicode characters. I.e. for arabic you would use an expression like (just an example)[php]$arabic=preg_match('/[\x{600}-\x{6FF}]/u', $content)[/php]For searching Japanese circled numbers 1-9[php]preg_match('/[\x{2460}-\x{2468}]/u', $str);[/php]
I don't know enough of regular expressions and Unicode to say much more about it. So I hope someone schooled in Reg ex and Unicode can help you any further.

Ronald
Reply