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. 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.'; } } ?> 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 } 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
Archived
This topic is now archived and is closed to further replies.