Jump to content

[SOLVED] validation to not allow russian, hebrew, chinese, and other non-english chars


Recommended Posts

PHP 5.2.5

I'm trying to validate some post vars so that the only thing that gets through is standard english keyboard type characters. So far I've added some Russian and Hebrew characters, but I'd expect that at some time I may encounter Chinese, Japanese, and many other characters. Here is what I have so far:

 

<?php
$badWord = array(
//phpfreaks converts the Russian and Hebrew chars, so I can't show you the chars, but they are just standard Russian and Hebrew chars.
);
foreach ($_POST as $dirtyString){
	foreach ($badWord as $unwanted){
		$testedString = strpos($dirtyString,$unwanted);
		if ($testedString){
			die();
		}
	}
}
?>

 

Is there a better way, and more complete way of doing what I want to do for all non-english chars?

not too sure if this helps but i found this...

 

preg_replace('/[^\x09\x0A\x0D\x20-\x7F\xC0-\xFF]/', '', $string);

 

at: http://www.sitepoint.com/blogs/2005/04/19/character-encodings-and-input/

 

... which says that will strip out any characters not in ISO-8859-1.

 

Also if you have the ability to enable functions with your PHP config look into "mbstring()" ...

 

 

Adam

I ended up using some simple regex that I created with regexbuddy. It seems to work, so I'm pleased, but hoping it doesn't somehow backfire on me later:

 

foreach ($_POST as $dirtyString){
	if (preg_match('/[^-\s A-Z0-9~!@#$%^&*()_+`=;:\'",<.>?|}{[\]\/\\\\]/i', $dirtyString)) {
		die();
	}
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.