开发者

PHP functions question

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++) {
      开发者_StackOverflow社区  $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;
}


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜