一:清除字串左右兩邊的空格和字符, 函數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教學有興趣的朋友有幫助。