Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you explode a string and said string does not contain the delimiter, does explode kick an error?

So I'm getting a section of the url with php. This section may or may not have trailing '?' values. So for example : I'm exploding the url : stackoverflow.com/questions and I want to get questions so i do something like explode ('/',$url). Pretend in this example the url might be as follows : stackoverflow.com/questions?query. I don't want that ending 'query' so I do explode ('?',$questionurl[no.]). However how does explode handle when the string doesn't contain the delimiter? Does it just return the string as it was or does it return false?

I've read through the php document and looked though questions in stack but can't quite seem to find this exact scenario.

I would be able to test this myself however the environment I'm working in is set up to work with drupal. As such it would require quite alot of messing around to be able to test one simple query, I figured it would more useful for all and future parties, here.

like image 283
Edward G-Jones Avatar asked Feb 07 '14 10:02

Edward G-Jones


People also ask

What is the difference between explode and split?

Both are used to split a string into an array, but the difference is that split() uses pattern for splitting whereas explode() uses string. explode() is faster than split() because it doesn't match string based on regular expression.

What does the explode () function do?

The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string.

What does the explode () function return?

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.


2 Answers

No errors are thrown - it will return an array with one element which is the entire string (remember that explode returns an array).

like image 195
cbreezier Avatar answered Oct 31 '22 05:10

cbreezier


From php.net explode() manual page:

If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

like image 34
Marcel Balzer Avatar answered Oct 31 '22 05:10

Marcel Balzer