在PHP中,可以利用substr()函数来分割字符串取前几位字符,该函数可以返回指定字符串的一部分,可以通过参数来规定从何处开始分割和要割取的长度,语法为“substr(字符串,0,前几位的位数)”。

本教程操作环境:windows10系统、PHP7.1版、DELL G3电脑
php怎么分割字符串取前几位
substr() 函数返回字符串的一部分。
注释:如果 start 参数是负数且 length 小于或等于 start,则 length 为 0。
语法
立即学习“PHP免费学习笔记(深入)”;
substr(string,start,length)
其中参数设置如下表所示:

示例如下:
";
echo substr("Hello world",1,8)."
";
echo substr("Hello world",0,5)."
";
echo substr("Hello world",6,6)."
";
echo substr("Hello world",0,-1)."
";
echo substr("Hello world",-10,-2)."
";
echo substr("Hello world",0,-6)."
";
echo substr("Hello world",-2-3)."
";
?>输出结果:

推荐学习:《PHP视频教程》











