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初 ...
随机推荐
- (3)-JSONObject的过滤设置
我们通常对一个json串和java对象进行互转时,经常会有选择性的过滤掉一些属性值.例如下面的类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- [label][Smarty]Smarty使用心得
Smarty模板引擎,使用smarty好处就是可以实现页面缓存,从而加快了初始化之后的页面访问速度. 某种程度上,smarty模板确保了template页面的代码整洁,避免了HTML标记与PHP的混合 ...
- 基于ASP.NET几十万数据几秒钟就可以导入到数据库中
/// <summary> /// 一.构建模拟数据存放于DataTable /// </summary> /// <returns>DataTable</r ...
- iOS 访问模拟器数据
1. 如果文件隐藏,进入命令行,输入: defaults write com.apple.finder AppleShowAllFiles -bool true 2. 进入 Finder,shift ...
- Django_Restframwork_序列号组件
第一种序列化方式. 第二种方法通过Model_to_dict方法进行创建 第三种方式序列号组件Serializers: 第四种方法序列化 五.ModelSerializer组件. POST校验 PU ...
- django系列8.5--使用装饰器(视图函数中)实现用户登录状态检验
views.py def session_auth(fn): def inner(request,*args,**kwargs): status = request.session.get('sess ...
- java学习笔记DOM4J解析(7)
DOM4J即Document Object Model for Java使用java技术以文档方式解析XML数据的模型. DOM4J是开源组织提供的一个免费的.强大的XML解析工具,如果开发者需要在项 ...
- 微信小程序 —— button按钮去除border边框
button默认有边框,边框用“border : none”去掉就不可以,边框依然存在, 使用 button::after{ border: none; } 来去除边框,边框就没了 wxml: < ...
- lambda 、 map 、filter 、reduce 及 reversed 常用函数
lambda 匿名函数 什么是lambda? lambda 操作符(或 lambda 函数)通常用来创建小巧的,一次性的匿名函数对象.它的基本语法如下: lambda arguments : expr ...
- Windows 如何完整备份驱动
软件:DriverBackUp 系统环境:Windows7 首先将DriverBackUp.exe放到桌面,然后运行,我们会看到提示信息提示我们驱动程序被备份到了D盘 然后我们会看到备份界面 这里我们 ...