billo Posted September 18, 2009 Share Posted September 18, 2009 Can I use a regex function to check a string and remove any character that you wouldn't find on a standard western keyboard? I tried preg function but it fails to detect some odd unprintable characters when they are pasted into the textbox: if(preg_match_all("/[^a-z0-9 \$_.,'()?!:\/;\&\#…\%\–]/i",$fldVal,$invalid)) Thanks in advance for any suggestions... Quote Link to comment https://forums.phpfreaks.com/topic/174649-solved-removing-non-standard-characters/ Share on other sites More sharing options...
MadTechie Posted September 18, 2009 Share Posted September 18, 2009 try this if (preg_match('/^[\20-\x7E]+$/', $fldVal)) { echo "valid"; } Quote Link to comment https://forums.phpfreaks.com/topic/174649-solved-removing-non-standard-characters/#findComment-920450 Share on other sites More sharing options...
billo Posted September 18, 2009 Author Share Posted September 18, 2009 thanks, but this doesn't catch accents as in crêpes or café any thoughts on how to weed them out? just to be clear, I want to check the text and chuck it out if I find anything other than A to Z, numbers and standard punctuation marks. So if someone tries to sneak in any weird character (pick one, lets say an angstrom Å=ångström), then I want to reject the text. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/174649-solved-removing-non-standard-characters/#findComment-920471 Share on other sites More sharing options...
MadTechie Posted September 18, 2009 Share Posted September 18, 2009 It does catch accents $fldVals = array("crêpes or café","test","Å=ångström"); foreach($fldVals as $fldVal) { echo $fldVal; if (preg_match('/^[\20-\x7E]+$/', $fldVal)) { echo ": valid<br>\n"; }else{ echo ": invalid<br>\n"; } } returns crêpes or café: invalid<br> test: valid<br> Å=ångström: invalid<br> as test was the only one with standard characters, its the only valid one Quote Link to comment https://forums.phpfreaks.com/topic/174649-solved-removing-non-standard-characters/#findComment-920523 Share on other sites More sharing options...
billo Posted September 18, 2009 Author Share Posted September 18, 2009 Quite right... some poorly crafted code I placed around your preg was the problem. Thanks very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/174649-solved-removing-non-standard-characters/#findComment-920913 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.