我知道这不能回答 OP 的问题,但想发表评论,因为此页面位于谷歌的顶部,用于多针 strpos。这是一个简单的解决方案(同样,这不是特定于 OP 的问题 - 抱歉):
$img_formats = array('.jpg','.png');
$missing = array();
foreach ( $img_formats as $format )
if ( stripos($post['timer_background_image'], $format) === false ) $missing[] = $format;
if (count($missing) == 2)
return array("save_data"=>$post,"error"=>array("message"=>"The background image must be in a .jpg or .png format.","field"=>"timer_background_image"));
如果将 2 个项目添加到 $missing 数组,则意味着输入不满足 $img_formats 数组中的任何图像格式。那时你知道你可以返回一个错误,等等。这可以很容易地变成一个小函数:
function m_stripos( $haystack = null, $needles = array() ){
//return early if missing arguments
if ( !$needles || !$haystack ) return false;
// create an array to evaluate at the end
$missing = array();
//Loop through needles array, and add to $missing array if not satisfied
foreach ( $needles as $needle )
if ( stripos($haystack, $needle) === false ) $missing[] = $needle;
//If the count of $missing and $needles is equal, we know there were no matches, return false..
if (count($missing) == count($needles)) return false;
//If we're here, be happy, return true...
return true;
}
回到我们使用 then 函数的第一个示例:
$needles = array('.jpg','.png');
if ( !m_strpos( $post['timer_background_image'], $needles ) )
return array("save_data"=>$post,"error"=>array("message"=>"The background image must be in a .jpg or .png format.","field"=>"timer_background_image"));
当然,在函数返回 true 或 false 之后你做什么取决于你。