志在指尖
用双手敲打未来

php substr(php substring函数详解)

phpsubstr

PHP中substr函数的作用是返回字符串的一部分,其语法为“substr(string,start,length)”,其参数string表明返回其中一部分的字符串,参数start表明在字符串的何处开始,参数length表明截取长度。
89b031211197a9b08e0d01fa6e9370ef.png
运用示例
echosubstr(“Helloworld”,10).”
“;
echosubstr(“Helloworld”,1).”
“;
echosubstr(“Helloworld”,3).”
“;
echosubstr(“Helloworld”,7).”
“;
echosubstr(“Helloworld”,-1).”
“;
echosubstr(“Helloworld”,-10).”
“;
echosubstr(“Helloworld”,-8).”
“;
echosubstr(“Helloworld”,-4).”
“;
?>
echosubstr(“Helloworld”,0,10).”
“;
echosubstr(“Helloworld”,1,8).”
“;
echosubstr(“Helloworld”,0,5).”
“;
echosubstr(“Helloworld”,6,6).”
“;
echosubstr(“Helloworld”,0,-1).”
“;
echosubstr(“Helloworld”,-10,-2).”
“;
echosubstr(“Helloworld”,0,-6).”
“;
echosubstr(“Helloworld”,-2-3).”
“;
?>

php

phpsubstring函数详解

substr()函数回来字符串的一部分。
substr(string,start,length)
参数:
1,string即你要截取的字符串
2,start即要截取的开端方位(0表明从早年往后数第一个字符开端,负数表明从从后往前数)
eg:start=1,表明从早年往后开端的第二个数开端截取,start=-1,表明从从后往前开端的第一(是第一不是第二哦)个数开端截取,
3,length当为正数时,为需要截取的长度;当为负数时,即理解为去掉结尾的几个字符
eg:length=3,表明截取三个长度;length=-2,即为去掉结尾的两个字符
示例(来自手册,解说确保三岁能看懂):
1,echosubstr(“Helloworld”,-10,-2);
输出:ellowor
解说:-10:表明啥?你应该清楚了吧!哈哈,当然是表明从后往前的第十个字符开端咯,咱们来数一下,数仔细咯,dlrow_(空格别忘了哦)olle,好停,就从你e开端截咯,前面的H不要咯
-2:又表明啥?你说我知道了,表明最好两个字符不要了呗!真棒!是那样的!所以ld也没你们啥事咯!
看完你是不是清楚了呢?
2,如果那个例一看不懂((⊙o⊙)?)那就再来看个简单入门的比如吧
echosubstr(“Helloworld”,6,6);
输出:world
解说:6:表明从第7个字符开端哦(777哦),当然是w咯;
6:当然表明截取6个长度的咯(什么,你说world只要5个长度,真机智,这都被你发现了,当然截取到结尾没得截了就回来了哦)
下面介绍php的substr函数用法
老是记不住,写一笔:
echosubstr(“Helloworld!”,6);输出world!,只要两个参数时,第一个参数表时字符串,第二个参数表明,从第6个开端,后面的全部截取,从0开端,空格也算一位
?>
echosubstr(“Helloworld!”,6,4);输出worl有三个参数时,第二个参数是起始方位,第三个参数为一共截取的长度
?>

未经允许不得转载:IT技术网站 » php substr(php substring函数详解)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载