|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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');
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 21:00:02 2025 UTC |
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); }