php strpos array

I needed a PHP function to check whether a string contained one of the array values. As we know strpos does’t accept input as an array so I had to tweak it  bit. Below is an example of what I did:

function strpos_array($string, $values) {
    $hits = 0; //will give number of values found in a string
    foreach ($values as $try) {
        $pos = strpos($string, $try);
        if ($pos !== false){
            $hits++; // you can do array_push here to get the values
        }
    }
    if ($hits==0) { //if none value from array was found returns false
        return false;
    }
    else { //I just needed a simple true/false boolean
       return true;
    }
}
This entry was posted in Web. Bookmark the permalink.

2 Responses to php strpos array

  1. Marci says:

    I was looking for something like this for my service. I would send a donation to you but I can’t read the paypal page.

  2. Thank you very much, greatly appreciated 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *