I was wondering if there is a more practical (shorter) way to do this, maybe with an array, as the number of variables and characters I need to check may increase later on.
Definitely. The following isnât the most efficient, but I think is pretty easy to follow:
/**
* Returns an array of blacklisted strings
* - A string is blacklisted only if it contains a substring
* @param [STR/ARR] $blacklist Either a string of characters to blacklist, or an array of substrings to blacklist
* @param [STR/ARR] $strings Either a string to check against, or a list of strings to check against
* @return [BOOL] Whether one of the strings is blacklisted
*/
function blacklisted($blacklist, $strings){
# Make $blacklist and $strings loopable
if(is_string($blacklist))
$blacklist = str_split($blacklist);
if(is_string($strings))
$strings = str_split($strings);
# Compare each substring to each string
foreach($blacklist as $checkedBlack){
foreach($strings as $checkedStr){
if(strpos($checkedStr, $checkedBlack) !== false)
return true;
}
}
return false;
}
Instead of doing a crazy manual, matrix comparison as you are now, simply pass in a list of blacklisted characters or an array of blacklisted substrings with a string or or list of strings to try and blacklist.
You then use it like this:
$vars = array('site', 'point', 'rules');
if(blacklisted('aeiou', $vars)){echo 'Has vowels';} # This will get called because each of the $vars has a vowel
if(blacklisted('jqz', $vars)){echo 'Has j, q, or z';} # this will not get called, because none of the $vars has j, q, or z
The great thing about building helper functions like this is that they are reusable. If you really want to get fancy, you can use a combination of array functions, which could potentially reduce all that to a line or two.
Sometimes itâs not about knowing all the functions, but how to hook-and-crook your way into getting them to do what you want preg_grep is designed to find pattern matches in an array; but with careful manipulation of the regex pattern, you can make it fit the find-one-of-many-chars function.
Thanks, but I guess that covers the case where you need to match two backslashes consequently (\\). I need to match just one backslash (\). I tried that anyway, and it didnât work. I guess I will continue with the long if statement I have in the first post.
I definitely donât mean to sound arrogant, but after seeing all the back and forth Iâm going to resuggest the helper function as itâs pretty open (you can pass strings or arrays of strings), compare characters or sub strings, works for the ", ', and , and at least for this case is much more readable:
function blacklisted($blacklist, $strings){
# Make $blacklist and $strings loopable
if(is_string($blacklist))
$blacklist = str_split($blacklist);
if(is_string($strings))
$strings = str_split($strings);
# Compare each substring to each string
foreach($blacklist as $checkedBlack){
foreach($strings as $checkedStr){
if(strpos($checkedStr, $checkedBlack) !== false)
return true;
}
}
return false;
}
if(blacklisted([$char1, $char2, $char3], [$var1, $var2, $var3])){
echo 'fail';
} else {
echo 'pass';
}
The helper function way (whether you use regex or loops) is a little more future proof as well, because if later you decide that you also need to know which strings were bad you can easily just return those strings instead of just true/false without rewriting your logic. Even if you use regex, with a helper function you can just change preg_grep to another method.