php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62051 missing function: mb_str_split()
Submitted: 2012-05-17 06:26 UTC Modified: 2012-06-02 06:10 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: info at javabar dot de Assigned: moriyoshi (profile)
Status: Wont fix Package: mbstring related
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: info at javabar dot de
New email:
PHP Version: OS:

 

 [2012-05-17 06:26 UTC] info at javabar dot de
Description:
------------
The multibyte version of str_split(), which splits a string into an array of chunks, does not seem to be implementated yet.

The name should be: mb_str_split()


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-17 11:50 UTC] laruence@php.net
-Package: Unknown/Other Function +Package: mbstring related
 [2012-05-19 06:48 UTC] laruence@php.net
-Assigned To: +Assigned To: moriyoshi
 [2012-05-19 06:48 UTC] laruence@php.net
moriyoshi sama, what do you think about this FR?  thanks
 [2012-06-02 06:10 UTC] moriyoshi@php.net
-Status: Assigned +Status: Wont fix
 [2012-06-02 06:10 UTC] moriyoshi@php.net
I have the impression that mb_split() pretty much does the expected work, unless 
we really want a counterpart of every byte-wise string function in mbstring...
 [2013-07-04 15:05 UTC] jan dot bouvrie at gmail dot com
@moriyoshi

array str_split (string $string[, int $split_length = 1 ])
Converts a string to an array. 

array mb_split (string $pattern, string $string [, int $limit = -1 ])
Split a multibyte string using regular expression pattern and returns the result as an array. 


Performance-wise, I wouldn't say mb_split (using a regular expression) is the same as str_split? Also, I'm curious how to call mb_split with a proper regexp so that it simulates str_split?

Example:
<?php
		function mb_str_split($str, $length = 1)
		{
		  if ($length < 1) return FALSE;
		  $result = array();
		  for ($i = 0; $i < mb_strlen($str); $i += $length) {
			$result[] = mb_substr($str, $i, $length);
		  }
		  return $result;
		}

mb_internal_encoding('UTF-8'); 
mb_regex_encoding('UTF-8');  

$test='әӘ火车票abc';
print_r(preg_split('/(?<!^)(?!$)/u', $test));
print_r(mb_str_split($test));
print_r(mb_split('/./',$test));
?>

outputs:
Array
(
    [0] => ә
    [1] => Ә
    [2] => 火
    [3] => 车
    [4] => 票
    [5] => a
    [6] => b
    [7] => c
)
Array
(
    [0] => ә
    [1] => Ә
    [2] => 火
    [3] => 车
    [4] => 票
    [5] => a
    [6] => b
    [7] => c
)
Array
(
    [0] => әӘ火车票abc
)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jan 05 01:01:28 2025 UTC