I'm fairly new to PHP functions I really dont know what the bottom functions do, can some one give an explanation or working example explaining the functions below. Thanks.
PHP functions.
function mbStringToArray ($str) {
if (empty($str)) return false;
$len = mb_strlen($str);
$array = array();
for ($i = 0; $i < $len; $i++) {
$array[] = mb_substr($str, $i, 1);
}
return $array;
}
function mb_chunk_split($str, $len, $glue) {
if (empty($str)) return false;
$array = mbStringToArray ($str);
$n = 0;
$new = '';
foreach ($array as $char) {
if ($n < $len) $new .= $char;
elseif ($n == $len) {
$new .= $glue . $char;
$n = 0;
}
$n++;
}
return $new;
}
asked Jan 14, 2011 at 0:20
HELP
14.6k22 gold badges70 silver badges102 bronze badges
2 Answers 2
The first function takes a multibyte string and converts it into an array of characters, returning the array.
The second function takes a multibyte string and inserts the $glue string every $len characters.
answered Jan 14, 2011 at 0:24
JSBձոգչ
41.6k19 gold badges106 silver badges173 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
jrn.ak
This could be used to insert soft-wraps into long strings that don't contain natural word-wrap locations.
function mbStringToArray ($str) { // $str is a function argument
if (empty($str)) return false; // empty() checks if the argument is not equal to NULL (but does exist)
$len = mb_strlen($str); // returns the length of a multibyte string (ie UTF-8)
$array = array(); // init of an array
for ($i = 0; $i < $len; $i++) { // self explanatory
$array[] = mb_substr($str, $i, 1); // mb_substr() substitutes from $str one char for each pass
}
return $array; // returns the result as an array
}
That should help you to understand the second function
answered Jan 14, 2011 at 0:27
Boris Guéry
47.6k8 gold badges56 silver badges88 bronze badges
Comments
lang-php
chunk_split. php.net/chunk_split multi-byte means they can deal with UTF-8 strings in which a character can consist of more than one byte