JSHINER Posted December 20, 2008 Share Posted December 20, 2008 I know there's a function for this I just can't think of it... been going nonstop for way too long on this project... $invalidCharacters = array(".", "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=", "{", "}", "[", "]", "|", "/", "'", ":", ";", ">", "<", ".", ","); How can I do something for if an $invalidCharacter is found in $variable, it returns something. Quote Link to comment https://forums.phpfreaks.com/topic/137845-solved-determine-if-characters-are-in-a-variable/ Share on other sites More sharing options...
genericnumber1 Posted December 20, 2008 Share Posted December 20, 2008 There might be a more efficient way, but... <?php $invalidCharacters = array(".", "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=", "{", "}", "[", "]", "|", "/", "'", ":", ";", ">", "<", ".", ","); $string = 'blahblah'; foreach($invalidCharacters as &$char) { if(strpos($string, $char) !== false) { echo 'Invalid character found.'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137845-solved-determine-if-characters-are-in-a-variable/#findComment-720420 Share on other sites More sharing options...
Mark Baker Posted December 20, 2008 Share Posted December 20, 2008 if ($string == $str_replace($invalidCharacters,'',$string)) { // valid } else { // $string contains one or more invalid characters } Quote Link to comment https://forums.phpfreaks.com/topic/137845-solved-determine-if-characters-are-in-a-variable/#findComment-720423 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.