Req #67659 [Com]: Support array of delimiters in explode()

From: Date: Mon, 21 Jul 2014 12:29:38 +0000
Subject: Req #67659 [Com]: Support array of delimiters in explode()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-186763@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 Comment by: anon at anon dot anon Reported by: arcadius at mail dot ru Summary: Support array of delimiters in explode() Status: Open Type: Feature/Change Request Package: Unknown/Other Function PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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); } Previous Comments: ------------------------------------------------------------------------ [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 (#186763) next »