首页 >后端开发 >php教程 >php字符串函数(1)

php字符串函数(1)

WBOY
WBOY原创
2016-08-08 09:24:201127浏览

一:清除字符串左右两边的空格和字符, 函数 trim()

<code><span>$str</span> = <span>"   hello,word  "</span>;
<span>echo</span><span>$str</span>.<span>"<br>"</span>;
<span>echo</span> trim(<span>$str</span>).<span>"<br>"</span>;

<span>$str</span> = <span>"@黎明hello,word@"</span>;
<span>echo</span> ltrim(<span>$str</span>).<span>"<br>"</span>;
<span>echo</span> rtrim(<span>$str</span>);
</code>

结果如下,ltrim()清除左侧的空格和字符,rtrim()清除右侧的空格和字符

二、转义字符串的函数 / addcslashes(str,ch)和stripcslashes(str);

<code><span>$str</span> = <span>"select * from data where name ='黎明'"</span>;
<span>$strm</span> = addcslashes(<span>$str</span>, <span>"'"</span>);
<span>$strl</span> = stripcslashes(<span>$str</span>);
<span>echo</span><span>$str</span>.<span>"<br>"</span>;
<span>echo</span><span>$strm</span>.<span>"<br>"</span>;
<span>echo</span><span>$strl</span>;</code>

addcslashes(str,ch)函数是将字符串内的一些特定字符前加入“/”,str指需要被转义的字符串,ch指被转义的字符。stripcslashes(str)函数还原被addcslashes函数转义的字符串

三、检索字符串函数/strstr()函数和strchr()函数

<code><span>$str</span> = <span>"abc@123.com"</span>;
<span>$tem</span> = strchr(<span>$str</span>,<span>"@"</span>);
<span>$tep</span> = strstr(<span>$str</span>, <span>"@"</span>);
<span>$ten</span> = strstr(<span>$str</span>, <span>"@"</span>,<span>true</span>);
<span>echo</span><span>$str</span>.<span>"<br>"</span>;
<span>echo</span><span>$tem</span>.<span>"<br>"</span>;
<span>echo</span><span>$tep</span>.<span>"<br>"</span>;
<span>echo</span><span>$ten</span>;</code>

strstr(str,ch)。str被检索的字符串,ch指要检索的字符,如果设置true的话,会返回被检索字符的左边字符。如不设置,则返回右边字符。strchr()函数返回右边字符。

以上就介绍了php字符串函数(1),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn