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

3

14

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.

Activism answered 7/2, 2014 at 10:54 Comment(4)
No, it returns an array with either no elements or a single element depending on the value of the limit argument.... exactly as described in the [documentation(php.net/manual/en/function.explode.php)... "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."Poulter
So it returns the sting in an array as it was. So I should call $url[0].Activism
FYI - you can make a test.php file anywhere on your server, put code inside <?php ?> tags and it will work. It has nothing to do with Drupal.Photophobia
depending on how you look at it, a NOTICE could be issued by PHP (only if you are unwrapping the results) as seen here (although technically in PHP's world Notice is different from error)Lise
W
12

Please read the php docs. It exists for a reason.

https://www.php.net/explode

Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.

If delimiter is an empty string (""), explode() will return FALSE. 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.

Weyermann answered 7/2, 2014 at 10:58 Comment(4)
I think its the 'negative limit' that threw me, I wasn't sure exactly what it meant by that.Activism
Read the manual page and take a look at the example where limit is used (third parameter). Then you'll understand ;)Perforce
If you don't understand the parameters used in a php function then you have to read the parameters section from the docs of the representative function.Weyermann
You can use array_pad to make the resulting array as long as you need. This will at least get rid of the error. E.g., list($var1, $var2) = array_pad(explode("/", $url), 2, null);Othilie
F
7

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

Fiasco answered 7/2, 2014 at 10:56 Comment(3)
it should be a comment not answerGeographical
Sorry, I'm relatively new here. Is it too simple to be an answer? Is there a limit on answer simplicity on SO?Fiasco
@SatishSharma No, this one is better than the accepted answer, which leads people to believe that an empty array will be returned.Peggi
P
1

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.

Perforce answered 7/2, 2014 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.