note 117112 added to function.str-split

From: Date: Thu, 16 Apr 2015 07:14:34 +0000
Subject: note 117112 added to function.str-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-202280@lists.php.net to get a copy of this message
Here is a better version of queremy@gmail.com's solution. It has the exact same interface as str_split, but works with any UTF-8 string. <?php if (!function_exists('mb_str_split')) { /** * Converts an UTF-8 string to an array. * * E.g. mb_str_split("Hello Friend"); * returns ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] * * @param string $string The input string. * @param int $split_length Maximum length of the chunk. If specified, the returned array will be broken down * into chunks with each being split_length in length, otherwise each chunk will be one character in length. * @return array|boolean * - * - If the split_length length exceeds the length of string, the entire string is returned * as the first (and only) array element. * - False is returned if split_length is less than 1. */ function mb_str_split($string, $split_length = 1) { if ($split_length == 1) { return preg_split("//u", $string, -1, PREG_SPLIT_NO_EMPTY); } elseif ($split_length > 1) { $return_value = []; $string_length = mb_strlen($string, "UTF-8"); for ($i = 0; $i < $string_length; $i += $split_length) { $return_value[] = mb_substr($string, $i, $split_length, "UTF-8"); } return $return_value; } else { return false; } } } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 86.101.224.185 ---- Manual Page -- http://php.net/manual/en/function.str-split.php Edit -- https://master.php.net/note/edit/117112 Del: integrated -- https://master.php.net/note/delete/117112/integrated Del: useless -- https://master.php.net/note/delete/117112/useless Del: bad code -- https://master.php.net/note/delete/117112/bad+code Del: spam -- https://master.php.net/note/delete/117112/spam Del: non-english -- https://master.php.net/note/delete/117112/non-english Del: in docs -- https://master.php.net/note/delete/117112/in+docs Del: other reasons-- https://master.php.net/note/delete/117112 Reject -- https://master.php.net/note/reject/117112 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202280) next »