开发者

Search through a string for any one of the items in an Array

Here's my search array:

$aExt = array('png','jpg','gif');

I want to search through the variable:

$sIcon

How can I search if the variable includes anything in开发者_Go百科 this array using PHP?

It's like in inverted in_array:

strstr($sIcon, $aExt) <- Can second arg be an array?


You can use foreach to iterate over the elements in the array and then use strpos to see if the key is in the content of the variable:

foreach($aExt as $key) {
  if(strpos($sIcon, $key) !== false) {
    echo sprintf("%s is in the variable", $key);
  }
}

Looking at the names of your variables I think you're trying to figure out the extension of a file name. You can easily find out the extension of a file using the following:

$ext = pathinfo($filename, PATHINFO_EXTENSION);

In your case:

$ext = pathinfo($sIcon, PATHINFO_EXTENSION);
if(in_array($ext, $aExt)) {
  echo "Valid icon!";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜