Reputation:
The main string contains parts which are separated by #
I need to check if the incoming string contains any of the banned keys.
In real example the keys are made by md5($var), each key 32 character length. Number of keys are variable in both $banned_keys and $incoming_keys1
$banned_keys = 'abc1#abc2#abc3#abc4';
$incoming_keys1='asd1#asd2#asd3#asd4'; //should pass no banned key found
$incoming_keys2='asd1#asd2#asd3#abc3'; //Should fail, contains banned key 'abc3'
Upvotes: 0
Views: 1221
Reputation: 450
Try looking here:
http://www.webcheatsheet.com/php/regular_expressions.php#match
and for the Regular Expression..
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
I'm still new at using Regular Expression..
I guess for your problem, the pattern would be...
/abc1|abc2|abc3|abc4/g
then the could should be..
<?php
$subject = "asd1#asd2#asd3#asd4";
$pattern = '/abc1|abc2|abc3|abc4/g';
if (preg_match($pattern, $subject, $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
?>
The preg_match() function returns 1 if a match is found and 0 otherwise.
Upvotes: 0
Reputation: 6571
Try following:
$banned_keys_array = explode("#", $banned_keys);
$incoming_keys_arr = explode("$", $incoming_keys);
$is_valid_key = true;
foreach($incoming_keys_arr as $key) {
if (in_array($key, $banned_keys_array) {
// Invalid key found;
$is_valid_key = false;
break;
}
}
// check $is_valid_key here
Upvotes: 0
Reputation: 4339
You could 'explode' the strings to transform them into arrays and then match the array entries (if the intersection contains any entries, at least one banned key is provided):
$banned_keys_ary = explode('#', $banned_keys);
$incoming_keys_ary = explode('#', $incoming_keys);
if (count(array_intersect($incoming_keys_ary, $banned_keys_ary)) > 0)
{
// fail, at least one banned key found
}
else
{
// pass, no banned keys found
}
Upvotes: 0
Reputation: 5316
Try converting the strings to arrays using explode('#',$string)
for both the incoming and the banned keys, then use in_array() to check.
For example
$banned = explode('#','abc1#abc2#abc3#abc4');
$incoming = explode('#','asd1#asd2#asd3#asd4');
if(in_array($banned,$incoming)){
//fail
}
There may be a better way, but this should work :)
Upvotes: 0
Reputation: 5229
$banned_array = explode('#', $banned_keys);
$incoming_array = explode('#', $incoming_keys);
// compute intersection of two sets
if( array_intersect($banned_array, $incoming_array) )
...
Upvotes: 3