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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: arcadius at mail dot ru
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 06:01:27 2025 UTC