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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: empaingeo at hotmail dot com
New email:
PHP Version: OS:

 

 [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 Feb 27 15:01:28 2020 UTC