I’ve written this function:
function contain_special($string){
# -- Check For Any Special Chars --
if(preg_match('/[^a-z0-9]/',$string)){
# - Special Chars Were Found -
return true;
}//end of special chars found
else{
# - String Does Not Contain Special Chars -
return false;
}//end of else - does not contain special chars
}//end of function
To check if a string contains special chars.
The function is supposed to ignore alphanumeric chars and look for special chars. If found, return true, else, return false.
Now all works well when testing it with most special chars:
$text="sdfs-df";
var_dump(contain_special($text));//returns true because "-" was found
BUT, when I have a $ that is not in a certain position of the string, the function fails to pick it up:
$text="sdfsdf$";//this works
$text="sdf$sdf";//this does not work
$text="$sdfsdf";//this works
Any ideas on what I’m doing wrong here?
Take a look at
echo $text. You may not be using the string you think you are. Literal dollar signs often need to be escaped in double-quoted strings so that you’re not using the variable,$sdfsdf, for example.I’d recommend just using single quotes here.
http://php.net/manual/en/language.types.string.php