在空字符串上爆炸会将数组计数返回为1

时间:2021-08-01 22:05:17

explode on empty string returns array count as 1.

在空字符串上爆炸会将数组计数返回为1。

   $consName =explode("|",$docDet['doc_cons_filename']); 
   count($consName);

If there is some value in $docDet['doc_cons_filename'] like ab|cd|de then count($consName) returns 3.

如果$ docDet ['doc_cons_filename']中有一些值,如ab | cd | de则count($ consName)返回3。

But its returning 1 if $docDet['doc_cons_filename'] has empty value.

但是如果$ docDet ['doc_cons_filename']的值为空,则返回1。

is it possible to return count as 0 if we perform count(explode("|",$docDet['doc_cons_filename'])) where $docDet['doc_cons_filename'] = ""

如果我们执行count,则可以将count返回为0(explode(“|”,$ docDet ['doc_cons_filename']))其中$ docDet ['doc_cons_filename'] =“”

Can anyone help me with solution?

任何人都可以帮我解决问题吗?

2 个解决方案

#1


-3  

$arr = array();
$str = "yes you are!";
if($i = substr_count($str,"|"))
  $arr = explode("|", $str, $i+1);
echo count($arr);

#2


5  

The solution would be to count explicitly how many times separator is found within your string. See substr_count()

解决方案是明确计算字符串中找到分隔符的次数。请参阅substr_count()

#1


-3  

$arr = array();
$str = "yes you are!";
if($i = substr_count($str,"|"))
  $arr = explode("|", $str, $i+1);
echo count($arr);

#2


5  

The solution would be to count explicitly how many times separator is found within your string. See substr_count()

解决方案是明确计算字符串中找到分隔符的次数。请参阅substr_count()