Home  >  Article  >  php教程  >  PHP将字符分解为多个字符串的方法

PHP将字符分解为多个字符串的方法

WBOY
WBOYOriginal
2016-06-06 20:16:571677browse

这篇文章主要介绍了PHP将字符分解为多个字符串的方法,通过split进行正则匹配实现分割字符串的功能,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了PHP将字符分解为多个子串的方法。分享给大家供大家参考。具体实现方法如下:

分解字符串主要通过Split()函数实现,该函数用于指把一个字符串通过指定的字符分解为多个子串,并分别存入数组中。其语法声明如下:
array split(string pattern,string str[,int limit]);
其中的参数:
pattern:用于指定作为分解标识的符号,,注意该参数区分大小写。
str:欲处理的字符串。
limit:返回分解子串个数的最大值,缺省时为全部返回。

示例如下:

使用split()函数将字符串“2006-10-12 16:50:49”分解为年月日时分秒子串,程序代码如下:

复制代码 代码如下:

$date="2006-10-12 16:50:49";
list($year,$month,$day,$hour,$minute,$second)=split('[-: ]',$date);
echo"北京时间:{$year}年{$month}月{$day}日{$hour}时{$minute}分{$second}秒";
?>


本示例的运行结果为:北京时间:2006年10月12日16时50分49秒

希望本文所述对大家的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn