PHP检查字符串是否在数组中

in_array — 该函数检查某个字符串是否存在于数组中

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Searches haystack for needle using loose comparison unless strict is set.
 

下面分享一个使用in_array的例子, 该例子,检查给定的文件扩展名是否在给定的数组中。

<?php
$ext = "PPT";
$ext = strtolower($ext);
$file_ext = array("doc", "ppt", "xls", "docx","xlsx","pptx","txt");
	if (in_array($ext, $file_ext)) {
		echo "文档类型";
	}else{
		echo "其他类型";
	}
?>

posted on 2012-03-25 22:37  IT技术畅销书  阅读(927)  评论(0编辑  收藏  举报

导航