0

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;
}
1
  • I assume they form a multi-byte version of 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 Commented Jan 14, 2011 at 0:22

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This could be used to insert soft-wraps into long strings that don't contain natural word-wrap locations.
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.