Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explode on empty string returns array count as 1 [closed]

Tags:

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?

like image 676
Devswa Avatar asked Feb 27 '12 18:02

Devswa


People also ask

What happens if you split an empty string?

If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.

What does the explode () function return?

explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.

What does the explode () function do?

The explode function is utilized to "Split a string into pieces of elements to form an array". The explode function in PHP enables us to break a string into smaller content with a break. This break is known as the delimiter.

Which function breaks string into array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

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

like image 115
Mchl Avatar answered Oct 04 '22 02:10

Mchl