摘要:将字符串转换为数组将一个字符串转换为数组。如果指定了可选的参数,返回数组中的每个元素均为一个长度为的字符块,否则每个字符块为单个字符。如果参数超过了超过了字符串的长度,整个字符串将作为数组仅有的一个元素返回。
str_split
</>复制代码
(PHP 5, PHP 7)
str_split — Convert a string to an array
str_split — 将字符串转换为数组
Description
</>复制代码
array str_split ( string $string [, int $split_length = 1 ] )
//Converts a string to an array.
//将一个字符串转换为数组。
Parameters
string
The input string.
输入字符串。
split_lengthMaximum length of the chunk.
每一段的长度。
Return ValuesIf the optional split_length parameter is 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.
如果指定了可选的 split_length 参数,返回数组中的每个元素均为一个长度为 split_length 的字符块,否则每个字符块为单个字符。
FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.
如果 split_length 小于 1,返回 FALSE。如果 split_length 参数超过了 string 超过了字符串 string 的长度,整个字符串将作为数组仅有的一个元素返回。
Examples</>复制代码
h;[1] => e;[2] => l;[3] => l;[4] => o;[5] => ;[6] => w;[7] => o;[8] => r;[9] => l;[10] => d
print_r( str_split( $str ) );
/*
* [0] => he
* [1] => ll
* [2] => o
* [3] => wo
* [4] => rl
* [5] => d
*/
print_r( str_split( $str, 2 ) );
/*
* [0] => hello
* [1] => worl
* [2] => d
*/
print_r( str_split( $str, 5 ) );
//PHP Warning: str_split(): The length of each segment must be greater than zero in file on line 28
//print_r(str_split($str,-5));
////////////////////////////////////////////////////////////////////////
function str_split_unicode( $str, $l = 0 ) {
if ( $l > 0 ) {
$ret = array();
$len = mb_strlen( $str, "UTF-8" );
for ( $i = 0; $i < $len; $i += $l ) {
$ret[] = mb_substr( $str, $i, $l, "UTF-8" );
}
return $ret;
}
return preg_split( "//u", $str, - 1, PREG_SPLIT_NO_EMPTY );
}
//[0] => 一
//[1] => 切
//[2] => 皆
//[3] => 文
//[4] => 件
print_r( str_split_unicode( "一切皆文件" ) );
//[0] => 一切皆
//[1] => 文件
print_r( str_split_unicode( "一切皆文件", 3 ) );
//[0] => 一�
//[1] => ���
//[2] => �文
//[3] => 件
print_r( str_split( "一切皆文件", 4 ) );
////////////////////////////////////////////////////////////////
$spl1 = str_split( "Long" );
echo count( $spl1 ) . PHP_EOL; // 4
//[0] => L
//[1] => o
//[2] => n
//[3] => g
print_r( $spl1 );
$spl2 = str_split( "X" );//1
echo count( $spl2 ) . PHP_EOL;
//[0] => X
print_r( $spl2 );
$spl3 = str_split( "" );
echo count( $spl3 ) . PHP_EOL;//1
//[0] =>
print_r( $spl3 );
$spl4 = str_split( 23 );
echo count( $spl4 ) . PHP_EOL;//2
//[0] => 2
//[1] => 3
print_r( $spl4 );
$spl5 = str_split( 2.3 );
echo count( $spl5 ) . PHP_EOL;//3
//[0] => 2
//[1] => .
//[2] => 3
print_r( $spl5 );
$spl6 = str_split( true );
echo count( $spl6 ) . PHP_EOL;//1
//[0] => 1
print_r( $spl6 );
$spl7 = str_split( null );
echo count( $spl7 ) . PHP_EOL;//1
//[0] =>
print_r( $spl7 );
See
http://php.net/manual/zh/func...
All rights reserved文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/28346.html
摘要:字符串分解操作要进行分解的字符串分解的长度。获取字符串的长度函数要进行长度计算的字符串包括首尾空格获取字符串的子串要进行截取的字符串截取开始的字符位置可选,要截取的字符串长度。默认从开始到结尾,字符串的第一个位置为获取字符串的子串 一 print和echo print 1)语法 int print(str);//str--要输出的字符串,返回值永远为1 --语法1 p...
摘要:为数组示例说明在中将字符串替换为即可。返回其中如果的数组值比的数组值长,将中多出来的数组元素在中匹配的字符串替换为空串,返回。 字符串大小写转换 strtoupper(string $str) //把字符串全部转换成大写字母 strtolower(string $str) //把字符串全部转换成小写字母 ucfirst(string $str) //把字符串的首字母转换成大写 ucw...
摘要:栈和队列栈和队列和之前讲到的实战数据结构基础之双链表一样都是线性结构。来看基于数组的栈实现得益于强大的结构,我们轻而易举的写出来了栈的基本操作方法。专题系列基础数据结构专题系列目录地址主要使用语法总结基础的数据结构和算法。 栈和队列 栈和队列和之前讲到的实战PHP数据结构基础之双链表 一样都是线性结构。 栈有什么特点 栈遵循后进先出的原则(LIFO)。这意味着栈只有一个出口用来压入元素...
摘要:由于是按难易度排序的,因此本题是第一题。先把问题简化为中只有一个字符的情形,因为字符串可以看作是一个字符数组。这个函数的作用就是,根据闭包函数,过滤数组元素。要注意是字符串,需要先转换成数组才行。 771. Jewels and Stones 由于是按难易度排序的,因此本题是第一题。 题目链接 771. Jewels and Stones 题目分析 从第二个参数S中找第一个参数J 中出...
阅读 2292·2021-09-07 09:58
阅读 3484·2019-08-30 14:07
阅读 1366·2019-08-29 12:32
阅读 740·2019-08-29 11:06
阅读 3791·2019-08-26 18:18
阅读 3833·2019-08-26 17:35
阅读 1458·2019-08-26 11:35
阅读 688·2019-08-26 11:35