php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67659 Support array of delimiters in explode()
Submitted: 2014-07-21 12:07 UTC Modified: 2020-11-03 18:03 UTC
From: arcadius at mail dot ru Assigned:
Status: Suspended Package: Strings related
PHP Version: Irrelevant OS:
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 — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
35 - 10 = ?
Subscribe to this entry?

 
 [2014-07-21 12:07 UTC] arcadius at mail dot ru
Description:
------------
Let give several delimiters as array to explode() function to explode input string by any of them. Delimiters listed earlier have higher priority:
array(',', ', ') // Wrong!
array(', ', ',') // Right.

Test script:
---------------
<?php
function dump_path($path)
{
	$arr = explode( array('\\', '/'), $path );
	var_dump($arr);
}
dump_path('d:\\aaa\\bbb.txt');
dump_path('/etc/hosts');

function dump_name($name)
{
	$arr = explode( array(' ', '.'), $name );
	var_dump($arr);
}
dump_name('Albert Einstein');
dump_name('A.Einstein');
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-21 12:10 UTC] arcadius at mail dot ru
-Summary: Use array of delimiters in explode() +Summary: Support array of delimiters in explode()
 [2014-07-21 12:10 UTC] arcadius at mail dot ru
Changed title of the request.
 [2014-07-21 12:29 UTC] anon at anon dot anon
You can already do that (and a hell a lot of more) with regex. For the specific examples you gave:

    1. $arr = preg_split('/\/|\\\\/', $path);

    2. $arr = preg_split('/ |\./', $name );

In general:

    function explode_multi($delimiters, $string, $limit = -1) {
        $pattern = '/';
        foreach ($delimiters as $d) $pattern .= preg_quote($d, '/') . '|';
        $pattern[strlen($pattern) - 1] = '/';
        return preg_split($pattern, $string, $limit);
    }
 [2018-02-28 20:13 UTC] cmb@php.net
-Package: Unknown/Other Function +Package: Strings related
 [2020-11-03 18:03 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-11-03 18:03 UTC] cmb@php.net
This feature requires discussion for which this bug tracker is
unsuitable.  If anybody is still interested in this, please
forward the request to the internals mailing list[1].  For the
time being, I'm suspending this ticket.

[1] <https://www.php.net/mailing-lists.php#internals>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 02:01:29 2024 UTC