我这里有个密码:
if (in_array("Notecards", $array) || in_array("Poster", $array)){
echo "Match found";
}else{
echo "Match not found";
}我的问题是,海报可以是海报1,海报2,海报3,海报4等等。
如果说如果有海报那么空间,那么一个数字是不是在我的数组中?
发布于 2013-12-18 05:17:01
像这样使用preg_grep:
if (in_array("Notecards", $array) || preg_grep("/Poster\s\d+/", $array)){
echo "Match found";
}else{
echo "Match not found";
}我冒昧地假设,您可能有多个海报0-9,在这种情况下,\d+匹配一个或多个数字(海报10,海报800等)。如果只需要一个数字,请删除+。
发布于 2013-12-18 05:19:44
这是一个解决办法:
if (in_array("Notecards", $array) || substr_in_array("Poster", $array)){
echo "Match found";
}else{
echo "Match not found";
}
substr_in_array($str, $array){
$length = strlen($str);
foreach($array as $value){
if(substr($value, 0, $length) == $str){
return true;
}
}
return false;
}https://stackoverflow.com/questions/20650108
复制相似问题