0

I've been looking for a way to check if any of an array of values exists in a string, but it seems that PHP has no native way of doing this, so I've come up with the below.

My question - is there a better way of doing this, as this seems pretty inefficient? Thanks.

$match_found = false;
$referer = wp_get_referer();
$valid_referers = array(
    'dd-options',
    'dd-options-footer',
    'dd-options-offices'
);

/** Loop through all referers looking for a match */
foreach($valid_referers as $string) :

    $referer_valid = strstr($referer, $string);
    if($referer_valid !== false) :
        $match_found = true;
        continue;
    endif;

endforeach;

/** If there were no matches, exit the function */
if(!$match_found) :
    return false;
endif;
5
  • What contains $referer variable ? Commented Jan 9, 2013 at 12:43
  • do you want exact matches or partial matches ? Commented Jan 9, 2013 at 12:45
  • 4
    the continue; does not make much sense, as there is no more code inside the loop which could be skipped. If you only want to know if there is at least one match, use break; instead. This way the loop will stop at the first match. Commented Jan 9, 2013 at 12:46
  • Inside your foreach - you want to break once you have found the first matching referer (and not to continue). Edit: beaten by Yoshi :) Commented Jan 9, 2013 at 12:48
  • @Yoshi - Good spot on that one thanks - an erro in my part in not changing to break; after an earlier attempt! Commented Jan 9, 2013 at 15:54

3 Answers 3

3

Try with following function:

function contains($input, array $referers)
{
    foreach($referers as $referer) {
        if (stripos($input,$referer) !== false) {
            return true;
        }
    }
    return false;
}

if ( contains($referer, $valid_referers) ) {
  // contains
}
Sign up to request clarification or add additional context in comments.

1 Comment

Think I'll go for something like that. Thanks for the suggestion.
0

What about this:

$exists = true;
array_walk($my_array, function($item, $key) {
    $exists &= (strpos($my_string, $item) !== FALSE);
});
var_dump($exists);

This will check if any of the array values exists in the string. If only one is missing, You are given a false response. Should You need to find out which one are not present in the string, try this:

$exists = true;
$not_present = array();
array_walk($my_array, function($item, $key) {
    if(strpos($my_string, $item) === FALSE) {
        $not_present[] = $item;
        $exists &= false;
    } else {
        $exists &= true;
    }
});
var_dump($exists);
var_dump($not_present);

Comments

0

First of, alternate syntax is nice to use, but historically it's used in template files. Since it's structure is easily readable while coupling/decouping the PHP interpreter to interpolate HTML data.

Second, it's generally wise if all your code does is to check something, to immediately return if that condition is met:

$match_found = false;
$referer = wp_get_referer();
$valid_referers = array(
    'dd-options',
    'dd-options-footer',
    'dd-options-offices'
);

/** Loop through all referers looking for a match */
foreach($valid_referers as $string) :

    $referer_valid = strstr($referer, $string);
    if($referer_valid !== false) :
        $match_found = true;
        break; // break here. You already know other values will not change the outcome
    endif;

endforeach;

/** If there were no matches, exit the function */
if(!$match_found) :
    return false;
endif;

// if you don't do anything after this return, it's identical to doing return $match_found

Now as specified by some of the other posts in this thread. PHP has a number of functions that can help. Here's a couple more:

in_array($referer, $valid_referers);// returns true/false on match

$valid_referers = array(
    'dd-options' => true,
    'dd-options-footer' => true,
    'dd-options-offices' => true
);// remapped to a dictionary instead of a standard array
isset($valid_referers[$referer]);// returns true/false on match

Ask if you have any questions.

2 Comments

Thaks for the suggestion. Am I correct in thinking thought that in_array() would only check that $referrer exactly met one of the values in $valid_referers? If so, that won't work as $referer is a HTTP referer (so HTTP://www.somting.com/page?referrer=dd-options&page_id=65, for example).
in_array, is a strict comparison function. So yes, it's needle must be exactly equal to the value of an item from array. Also I'm uncertain what wp_get_referer does. I'm guessing it's a wordpress function but that's about it. You can get it in your example with $referer=$_GET['referrer']. Are these the only 3 valid options ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.