PHP中substr()与explode()函数用法分析_PHP

WBOY
Release: 2016-05-31 18:15:42
Original
784 people have browsed it

本文实例讲述了PHP中substr()与explode()函数用法。分享给大家供大家参考。具体方法如下:

substr(string,start,length):本函数将字符串 string 的第 start 位起的字符串取出 length 个字符,若 start 为负数,则从字符串尾端算起,若可省略的参数 length 存在,但为负数,则表示取到倒数第 length 个字符,实例代码如下:

代码如下:

echo substr("abcdef", 1, 3);  // 返回 "bcd"
echo substr("abcdef", -2);    // 返回 "ef"
echo substr("abcdef", -3, 1); // 返回 "d"
echo substr("abcdef", 1, -1); // 返回 "bcde"
?>


explode(separator,string,limit):本函数将字符串分割为数组 separator 规定在哪里分割,string 要分割的字符串,limit 可选,数组元素的最大数目,实例代码如下:

代码如下:

 $str = "abc|bck|fhq|nui";
   print(explode("|",$srt)); //输出array([0]=>abc,[1]=>bck,[2]=>fhq,[3]=>nui)
?>

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template