php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65396 Separators at the beginning of string are also exploded
Submitted: 2013-08-05 23:39 UTC Modified: 2013-08-10 22:23 UTC
From: empaingeo at hotmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: Irrelevant OS: Windows Vista
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
3 + 46 = ?
Subscribe to this entry?

 
 [2013-08-05 23:39 UTC] empaingeo at hotmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.explode#refsect1-function.explode-returnvalues
---

Hi, to reproduce the problem :



Test script:
---------------
<?php
	echo "<pre>";
	echo print_r(explode(' ', "    test1 test2"));
	echo "</pre>";
?>

Expected result:
----------------
Array
(
    [0] => test1
    [1] => test2
)
1

Actual result:
--------------
Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => test1
    [5] => test2
)
1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-07 20:36 UTC] mail+php at requinix dot net
Totally intended and often desirable behavior.

If you don't want those then trim() the spaces off first, or if you're worried 
about multiple spaces inside the string too ("test1   test2") then array_filter() 
the result.
 [2013-08-07 21:42 UTC] empaingeo at hotmail dot com
Hi, thanks for the reply. I will follow your advice.

Best regards.
 [2013-08-10 22:23 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2013-08-10 22:23 UTC] ab@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

or additionally to trim, preg_replace(',\s+,', ' ', $str)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Apr 02 10:01:23 2020 UTC