Issue
I need to check if a string contains illegal character and I was wondering if there is a way to do it through an array because I have an array character that are allowed. This is in PHP
The array just in case you want to see it:
$MsgAry1 = array("A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z","1","2","3","4","5","6","7","8",
"9","0",".",",","'","?","!"," ","_","-");
Thanks for any help.
Solution
You can use the str_split
and in_array
functions to do this, as thus:
$MsgAry1 = array("A","B","C"); //Add your actual array here
$testCase = "Hello, World!";
$testCase_arr = str_split($testCase);
foreach($testCase as $test)
{
if(!in_array($test, $MsgAry1))
{
echo "Error! Invalid Character!";
break(); //Or exit();, as needed
}
}
Answered By – Acharya Anurag
Answer Checked By – Mildred Charles (BugsFixing Admin)