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: 2018-02-28 20:13 UTC
From: arcadius at mail dot ru Assigned:
Status: Open Package: Strings related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Apr 05 02:01:23 2020 UTC