Skip to content

Instantly share code, notes, and snippets.

@fetus-hina
Created February 20, 2012 07:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fetus-hina/1868198 to your computer and use it in GitHub Desktop.
Save fetus-hina/1868198 to your computer and use it in GitHub Desktop.
mb_explode() function
<?php
/*
* Copyright (C) 2012 by HiNa <hina@bouhime.com>. All rights reserved.
*
* LICENSE
*
* This source file is subject to the 2-clause BSD License(Simplified
* BSD License).
*/
if(!function_exists('mb_explode')) {
function mb_explode($delimiter, $string, $limit = -1, $encoding = 'auto') {
if(!is_array($delimiter)) {
$delimiter = array($delimiter);
}
if(strtolower($encoding) === 'auto') {
$encoding = mb_internal_encoding();
}
if(is_array($string) || $string instanceof Traversable) {
$result = array();
foreach($string as $key => $val) {
$result[$key] = mb_explode($delimiter, $val, $limit, $encoding);
}
return $result;
}
$result = array();
$currentpos = 0;
$string_length = mb_strlen($string, $encoding);
while($limit < 0 || count($result) < $limit) {
$minpos = $string_length;
$delim_index = null;
foreach($delimiter as $index => $delim) {
if(($findpos = mb_strpos($string, $delim, $currentpos, $encoding)) !== false) {
if($findpos < $minpos) {
$minpos = $findpos;
$delim_index = $index;
}
}
}
$result[] = mb_substr($string, $currentpos, $minpos - $currentpos, $encoding);
if($delim_index === null) {
break;
}
$currentpos = $minpos + mb_strlen($delimiter[$delim_index], $encoding);
}
return $result;
}
}
@Video-VV
Copy link

Video-VV commented Mar 6, 2018

Sorry, but if I use the PHP instructions:
ini_set('display_errors', 1); error_reporting(E_ALL);

in such code (for example):
$cSimple_String = 'Hello word';
$cDelim = "";
$aSimple_Array = mb_explorer( $cDelim, $cSimple_String );

I receive a warning error:
Warning: mb_strpos() [function.mb-strpos]: Empty delimiter in mb_explode.function.php on line 33
33 if(($findpos = mb_strpos($string, $delim, $currentpos, $encoding)) !== false) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment