php strpos array

March 26, 2009 – 07:57

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
    }
}
  1. 3 Responses to “php strpos array”

  2. 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.

    By Marci on Oct 1, 2009

  3. Thank you very much, greatly appreciated :)

    By Piotr Zaniewicz on Oct 2, 2009

  4. thanks for sharing!!!

    By php thumbnailer on May 21, 2010

Post a Comment