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.

4
  • 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(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." Commented Feb 7, 2014 at 10:54
  • So it returns the sting in an array as it was. So I should call $url[0]. Commented Feb 7, 2014 at 10:56
  • 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. Commented Sep 13, 2016 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 (although technically in PHP's world Notice is different from error) Commented Oct 6, 2020 at 6:26

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I think its the 'negative limit' that threw me, I wasn't sure exactly what it meant by that.
Read the manual page and take a look at the example where limit is used (third parameter). Then you'll understand ;)
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.
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);
7

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

3 Comments

it should be a comment not answer
Sorry, I'm relatively new here. Is it too simple to be an answer? Is there a limit on answer simplicity on SO?
@SatishSharma No, this one is better than the accepted answer, which leads people to believe that an empty array will be returned.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.