sKunKbad Posted July 30, 2008 Share Posted July 30, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/117373-solved-validation-to-not-allow-russian-hebrew-chinese-and-other-non-english-chars/ Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 Just use a regex. Quote Link to comment https://forums.phpfreaks.com/topic/117373-solved-validation-to-not-allow-russian-hebrew-chinese-and-other-non-english-chars/#findComment-603693 Share on other sites More sharing options...
Adam Posted July 30, 2008 Share Posted July 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117373-solved-validation-to-not-allow-russian-hebrew-chinese-and-other-non-english-chars/#findComment-603694 Share on other sites More sharing options...
sKunKbad Posted July 30, 2008 Author Share Posted July 30, 2008 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/117373-solved-validation-to-not-allow-russian-hebrew-chinese-and-other-non-english-chars/#findComment-604077 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.