str_split() - 将字符串转换为数组 - php 字符串函数
str_split()
(PHP 5, PHP 7)
将字符串转换为数组
说明
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; $iThe 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 :
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)