23

explode on empty string returns array count as 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.

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

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

Can anyone help me with solution?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Devswa
  • 327
  • 2
  • 4
  • 12

2 Answers2

10

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

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • There are plenty on the linked page – Mchl May 20 '16 at 12:57
  • 2
    use array_filter() like this => count(array_filter($array));The array isn't empty. It has one element which contains a string which is 0 in length.You don't need to pass a second parameter to array_filter() because an empty string will evaluate to false (and hence be removed). – Parth kharecha Oct 11 '19 at 05:42
-2
$arr = array();
$str = "yes you are!";
if($i = substr_count($str,"|"))
  $arr = explode("|", $str, $i+1);
echo count($arr);
Cody Tookode
  • 862
  • 12
  • 22
  • `explode` returns false if the `delimiter` is blank, not the input string. Also, you need both parameters in `explode`. – gen_Eric Feb 27 '12 at 18:52
  • That is wrong, it only returns `FALSE` when the delimiter string is empty – klaustopher Feb 27 '12 at 18:53
  • you also have your variables mixed up. you save the result of explode to $str then check the count of $arr which is the original variable you are exploding. – Jonathan Kuhn Feb 27 '12 at 18:55
  • 1
    If there is no delimiter in the input string, `count` will still be 0 even the input string is not empty which I don't think is correct. Better answer: [Here](http://stackoverflow.com/questions/64570/explode-that-doesnt-return-empty-strings?rq=1) – pikachu0 Feb 19 '14 at 10:15
  • Topic starter didn't asked about empty delimiter and he didn't asked how to initiate empty array. He asked how to return zero from explode() on empty input string. My example showing you that there is no need to explode string without delimiter at all. Pay attention that If there is no delimiter in string, IF condition from example above will skip the explode line and echo empty array count which is 0. Anyway I agree with you that my example is not perfect, but I don't think that preg_split is a better way to solve this issue. – Cody Tookode Mar 06 '17 at 21:43
  • Maybe it would be a good idea to delete this answer, and you will get back the lost reputation. – Jose Manuel Abarca Rodríguez Jun 21 '18 at 20:31
  • It's not about reputation. If topic starter agree that my answer useless, I'll remove my answer. – Cody Tookode Jun 23 '18 at 08:46
  • `array_filter(explode("|", $string));` did the thing for me! It will filter empty array entries. – Karamellwuerfel Jun 27 '23 at 05:34