13

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.

Edward G-Jones
  • 575
  • 3
  • 11
  • 24
  • 3
    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(http://www.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." – Mark Baker Feb 07 '14 at 10:54
  • So it returns the sting in an array as it was. So I should call $url[0]. – Edward G-Jones Feb 07 '14 at 10:56
  • FYI - you can make a `test.php` file anywhere on your server, put code inside `` tags and it will work. It has nothing to do with Drupal. – Chris Baker Sep 13 '16 at 17:25
  • depending on how you look at it, a `NOTICE` could be issued by PHP (only if you are unwrapping the results) as seen [here](https://imgur.com/a/tGvrLz4) (although technically in PHP's world [Notice is different from error](https://stackoverflow.com/a/4624499/3679900)) – y2k-shubham Oct 06 '20 at 06:26

3 Answers3

12

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

http://be2.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.

kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51
  • 2
    I think its the 'negative limit' that threw me, I wasn't sure exactly what it meant by that. – Edward G-Jones Feb 07 '14 at 11:00
  • 2
    Read the manual page and take a look at the example where limit is used (third parameter). Then you'll understand ;) – Maarkoize Feb 07 '14 at 11:01
  • 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. – kasper Taeymans Feb 07 '14 at 11:02
  • 1
    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);` – Chris Middleton Nov 25 '14 at 21:54
6

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

cbreezier
  • 1,188
  • 1
  • 9
  • 17
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.

Maarkoize
  • 2,601
  • 2
  • 16
  • 34