我正在使用$text
和这个正则表达式计算preg_match_all
中一些特殊字符(如euro symbol)的数量:
preg_match_all('/[\[|\]|€\{|}|\\|\^|\||~]/u', $text);
出于某种奇怪的原因,php要求我提供第三个参数。但根据documentation of preg_match_all,它应该是可选的:
警告:preg_match_all()需要至少3个参数,给定2个。
如果我提供
PREG_PATTERN_ORDER
(甚至不知道为什么)我会得到:无法通过引用传递参数3。
那么,我的代码怎么了?如果需要,以下是整个功能:
public function getMessageCount($text)
{
$specials = preg_match_all('/[\[|\]|€\{|}|\\|\^|\||~]/u', $text)
$characters = strlen($text) + $specials;
if(in_array(strtolower($this->method), self::$classic_plans)) :
if($characters >= 0 && $characters <= 160) return 1;
if($characters >= 161 && $characters <= 306) return 2;
if($characters >= 307 && $characters <= 459) return 3;
if($characters >= 460 && $characters <= 612) return 4;
return 5;
endif;
if(in_array(strtolower($this->method), self::$basic_plans)) :
if($characters >= 0 && $characters <= 160) return 1;
if($characters >= 161 && $characters <= 312) return 2;
if($characters >= 313 && $characters <= 468) return 3;
if($characters >= 469 && $characters <= 624) return 4;
if($characters >= 625 && $characters <= 780) return 5;
if($characters >= 781 && $characters <= 936) return 6;
if($characters >= 937 && $characters <= 1092) return 7;
if($characters >= 1093 && $characters <= 1248) return 8;
return 9;
endif;
return in_array(strtolower($this->method), self::$zero_plans) ? 1 : null;
}
最佳答案
尽管第三个参数在5.4.0中变得可选,正如其他人已经说过的,但是即使您传递了第三个参数,您的代码也不会编译,因为您说您传递了preg_pattern_order标志,但是第三个参数应该是接收匹配的数组,第四个参数是flag。
使用类似于following的东西:
<?php
$dummy = array();
echo("Result = ".preg_match_all('/[\[|\]|x\{|}|\\|\^|\||~]/', $text, $dummy));
?>