Req #67659 [Opn->Sus]: Support array of delimiters in explode()

From: Date: Tue, 03 Nov 2020 18:03:08 +0000
Subject: Req #67659 [Opn->Sus]: Support array of delimiters in explode()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230092@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67659&edit=1 ID: 67659 Updated by: cmb@php.net Reported by: arcadius at mail dot ru Summary: Support array of delimiters in explode() -Status: Open +Status: Suspended Type: Feature/Change Request Package: Strings related PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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> Previous Comments: ------------------------------------------------------------------------ [2014-07-21 12:29:37] 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); } ------------------------------------------------------------------------ [2014-07-21 12:10:09] arcadius at mail dot ru Changed title of the request. ------------------------------------------------------------------------ [2014-07-21 12:07:24] 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'); ?> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=67659&edit=1

« previous php.bugs (#230092) next »