php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48692 error in response
Submitted: 2009-06-25 13:42 UTC Modified: 2009-06-26 23:15 UTC
From: estudiosima at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.10 OS: ubuntu 8.04
Private report: No CVE-ID: None
 [2009-06-25 13:42 UTC] estudiosima at gmail dot com
Description:
------------
if the string have a space in the begin of the string, takes the space as a word.

Reproduce code:
---------------
---
From manual page: function.explode
---

<?
phpinfo();
$a = " word1 word2";
$b = explode(" ",$a);
print_r($b);
print "<br>".count($b);

/* result:

Array ( [0] => [1] => word1 [2] => word2 )
3

*/
?>

Expected result:
----------------
Array ( [0]=>word1 [1]=>word2 )

Actual result:
--------------
Array ( [0] => [1] => word1 [2] => word2 )

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-25 19:59 UTC] sjoerd-php at linuxonly dot nl
Thank you for your bug report.

The behavior you describe is by design. If there are n delimeters, explode() returns n+1 elements. It splits the string using a space, and thus also splits using the first space. Because there is nothing before the first space, the first element of the array is empty.

In this case, you can first trim() the string to remove spaces.
 [2009-06-26 23:15 UTC] jani@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


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC