百科狗-知识改变命运!

str_split() - 将字符串转换为数组 - php 字符串函数

梵高2年前 (2023-11-21)阅读数 26#技术干货
文章标签字符串

str_split()

(PHP 5, PHP 7)

str_split() - 将字符串转换为数组 - php 字符串函数

将字符串转换为数组

说明

str_split(string $string[,int $split_length= 1] ): array

将一个字符串转换为数组。

参数

$string

输入字符串。

$split_length

每一段的长度。

返回值

如果指定了可选的$split_length参数,返回数组中的每个元素均为一个长度为$split_length的字符块,否则每个字符块为单个字符。

如果$split_length小于 1,返回FALSE。如果$split_length参数超过了$string超过了字符串$string的长度,整个字符串将作为数组仅有的一个元素返回。

范例

Example #1str_split()使用范例

以上例程会输出:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)
Array
(
    [0] => Hel
    [1] => lo
    [2] => Fri
    [3] => end
)

注释

Note:

在处理多字节字符时,str_split()会按字节数转换,而非字符数。

参见

  • chunk_split()将字符串分割成小块
  • preg_split()通过一个正则表达式分隔字符串
  • explode()使用一个字符串分割另一个字符串
  • count_chars()返回字符串所用字符的信息
  • str_word_count()返回字符串中单词的使用情况
  • for
A proper unicode string split;

$s = "Ilık süt"; // Mild milk
print_r(str_split($s, 3));
print_r(str_split_unicode($s, 3));
Array
(
  [0] => Il
  [1] => k 
  [2] => sü
  [3] => t
)
Array
(
  [0] => Ilı
  [1] => k s
  [2] => üt
)
A new version of "str_split_unicode" prev.

$s = 'Özgür Yazılım!'; // Open Source!
print_r(str_split_unicode($s));
print_r(str_split_unicode($s, 3));
Array
(
  [0] => Ö
  [1] => z
  [2] => g
  [3] => ü
  [4] => r
  [5] => 
  [6] => Y
  [7] => a
  [8] => z
  [9] => ı
  [10] => l
  [11] => ı
  [12] => m
  [13] => !
)
Array
(
  [0] => Özg
  [1] => ür 
  [2] => Yaz
  [3] => ılı
  [4] => m!
)
It's mentioned in the Return Values section above ("If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element"), but note that an input of empty string will return array(1) { [0]=> string(0) "" }. Interestingly an input of NULL will also return array(1) { [0]=> string(0) "" }.
Compare this with, say,  which will return array(0) { } for an input of empty string or NULL. I find this to be a bit more intuitive.
Hope this helps.
Version of str_split by rlpvandenberg at hotmail dot com is god-damn inefficient and when $i+$j > strlen($text) [last part of string] throws a lot of notice errors. This should work better:
  if(! function_exists('str_split'))
  {
    function str_split($text, $split = 1)
    {
      $array = array();
      
      for ($i = 0; $i 
The manual don't says what is returned when you parse a different type of variable.
This is the example:
Note that in atleast in PHP 5.5.9 (Zend Engine v2.5.0), str_split with an integer value as an argument may return unpredictable results.
If your number contains leading 0's, the result array is unprdictable as it may contain any number of digits from the argument or (mostly) just a 0.
Here are a list of possible values that might be returned:
-Interger

BUT 

Floating point numbers have their leading and trailing 0s cut off:

I'm not sure if this can be considered a bug, since this is due to how type conversion and casting works, so i just posted it here.
I've notced that this is how strval() works. Can anyone shed light into this?..
Here is what I use. I started with examples here but modified to my own version:
The previous suggestion is almost correct (and will only working for strlen=1. The working PHP4 function is:

function str_split($text, $split = 1){
  //place each character of the string into and array
  $array = array();
  for ($i=0; $i 
the fastast way (that fits my needs) to replace str_split() in php 4 i found is this:

i also tested the provided functions in the comments..
(the differences are 0.001 to 0.00001 sec)
I needed a function that could split a string from the end with any left over chunk being at the beginning of the array (the beginning of the string). 
I was looking for a function that would split a string into an array like str_split() and found Razor's function above. Just though that I would simplify the code a little.

Both mine and worksRazor's work well, I just prefer to use less code. I could have written one myself, but I was just being lazy.
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.
To split UTF-8 into 16-charcater slices you can use:

You can also use look-behind instead of look-ahead if you want the first (not the last) chunk to be the shortest.
here an equivalent function for unicode string :

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}