PHP之string之str_split()函数使用
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_length
- Maximum length of the chunk.
- 每一段的长度。
Return Values
- If 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
<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/3/7
* Time: 下午10:24
*/
$str = "hello world";
//[0] => 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
All rights reserved
PHP之string之str_split()函数使用的更多相关文章
- php str_split()函数 语法
php str_split()函数 语法 str_split()函数怎么用 php str_split()函数用于把字符串分割到数组中,语法是str_split(string,length),将字符串 ...
- PHP str_split() 函数
实例 把字符串 "Hello" 分割到数组中: <?php print_r(str_split("Hello")); ?>高佣联盟 www.cgew ...
- OC与c混编实现Java的String的hashcode()函数
首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ...
- string类find函数返回值判定
string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...
- C string.h 常用函数
参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...
- c++中string的常用函数说明
string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ...
- C++ string类及其函数的讲解
文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...
- PHP之string之explode()函数使用
explode (PHP 4, PHP 5, PHP 7) explode - Split a string by string explode - 使用一个字符串分割另一个字符串 Descripti ...
- C++string类常用函数
C++string类常用函数 string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初 ...
随机推荐
- 咏南中间件新增SQL日志
为了方便开发时跟踪调试SQL语句的执行情况,咏南中间件新增SQL日志,所有执行过的SQL都会写入SQL日志文件中. SQLDEBUG设为1,启用:设为0,停止写SQL日志.
- Lucene 分页搜索实现
Lucene中有两种分页查询方式 1.一次查询出大量数据,然后根据页码定位是哪个文档,其实就是暴力获取了 2.通过调用searchAfter来实现 我们都知道collect是lucene中对搜索到的文 ...
- EJB学习手记
周末两天,看了两天的ejb知识.公司有个转发消息的程序,里面是根据ejb/jms+cdi/event做的,这些之前没接触过. 总而言之,从中学到了很多东西,从ejb到webservice. jboss ...
- 转载WPF:创建你的第一个WPF项目
转载:http://www.cnblogs.com/pengjinyu/archive/2009/08/19/1549845.html
- cesium随笔 — 隐藏三维场景下方版权信息
上图中的版权信息相信很多人都想去掉,那么下面我将介绍一种简单粗暴的方法将其隐藏起来: .cesium-widget-credits { display: none!important; visibil ...
- zun 不能创建 docker 容器,报错: datastore for scope "global" is not initialized
问题:zun不能创建docker容器,报错:datastore for scope "global" is not initialized 解决:修改docker 服务配置文件 ...
- AtcoderExaWizards 2019题解
传送门 \(A\ Regular\ Triangle\) 咕咕 \(B\ Red\ or\ Blue\) 咕咕咕 \(C\ Snuke\ the\ Wizard\) 我可能脑子真的坏掉了-- 容易发现 ...
- ROS(URDF机器人建模)
新建功能包mbot_description 在功能包下新建文件config,launch,meshes,urdf. 在launch文件夹下新建文件display_mbot_base_urdf.laun ...
- 阿里云ros实例
模板文件 { "ROSTemplateFormatVersion": "2015-09-01", "Parameters": { " ...
- [Swift实际操作]七、常见概念-(14)使用UIColor设置界面组件的颜色属性
打开移动应用程序,不可避免的需要和颜色打交道.本文将为你演示颜色对象的使用. 首先导入需要使用到的界面工具框架 import UIKit 通过UIColor的属性,可以获得橙色.右侧的实时反馈区,显示 ...