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()函数使用的更多相关文章

  1. php str_split()函数 语法

    php str_split()函数 语法 str_split()函数怎么用 php str_split()函数用于把字符串分割到数组中,语法是str_split(string,length),将字符串 ...

  2. PHP str_split() 函数

    实例 把字符串 "Hello" 分割到数组中: <?php print_r(str_split("Hello")); ?>高佣联盟 www.cgew ...

  3. OC与c混编实现Java的String的hashcode()函数

    首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ...

  4. string类find函数返回值判定

     string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...

  5. C string.h 常用函数

    参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...

  6. c++中string的常用函数说明

    string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ...

  7. C++ string类及其函数的讲解

    文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...

  8. PHP之string之explode()函数使用

    explode (PHP 4, PHP 5, PHP 7) explode - Split a string by string explode - 使用一个字符串分割另一个字符串 Descripti ...

  9. C++string类常用函数

    C++string类常用函数 string类的构造函数:string(const char *s);    //用c字符串s初始化string(int n,char c);     //用n个字符c初 ...

随机推荐

  1. [label][转载][JavaSript]querySelectorAll 方法相比 getElementsBy 系列方法有什么区别?

     轉載出處: http://www.zhihu.com/question/24702250 querySelectorAll 相比下面这些方法有什么区别? getElementsByTagName g ...

  2. Android-SurfaceView生命周期

    SurfaceView的生命周期,和 Activity生命周期,Service生命周期,BroadcastReceiver生命周期,等,不一样: 因为SurfaceView显示的是(视频画面,游戏画面 ...

  3. Android-进程理解/进程的优先级别

    进程理解 Android系统最小的控制单元是:进程 process 应用/CPU最小的控制单元是:线程 thread 一个应用一个 process 进程 一个应用一个 package(包是唯一的) 一 ...

  4. GlusterFS 一

    GlusterFS 一 1 安装源 yum install centos-release-gluster312.noarch 列出所有可用安装包yum list gluster* 安装glusterf ...

  5. shell中调用jenkins API批量运行历史任务

    shell中调用jenkins API批量运行jenkins带参数的任务: #!/bin/sh #startdate=20150127 startdate=20150201 while [ " ...

  6. dokcer 的export 、improt和save 、load

    export .improt 是对容器操作也就是类似于虚拟机的快照 save .load 是针对于镜像操作的..

  7. 62 不同路径 leetcode JAVA

    题目: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 问 ...

  8. Jquery、Ajax实现新闻列表页分页功能

    前端页面官网的开发,离不开新闻列表,新闻列表一般都会有分页的功能,下面是我自己总结加查找网上资料写的一个分页的功能,记录一下. 首先,官网的开发建立在前后端分离的基础上: 再有,后端小伙伴们提供列表页 ...

  9. 部署LVS-NAT群集

    案例环境 LVS调度器作为Web服务器池的网关,LVS两块网卡,分别连接内外网,外网地址172.16.16.172.24,同时也作为整个群集的VIP,内网地址为192.168.7.21-24/24,是 ...

  10. 基本项目框架搭建 sqlserver druid配置

    1.  我的连接池采用的是阿里云的druid的连接池,工具是IDEA 框架是springboot+maven 以下是我的项目框架结构 2. pom  中配置 <?xml version=&quo ...