This article analyzes the usage of common string processing functions in PHP. Share it with everyone for your reference, the details are as follows:
<?php $s = "hello world"; //整理 echo 'trim(); ltrim(); rtrim()'; echo '<br />'; echo '长度为: '.strlen($s); echo '<br />'; //大小写 echo '首字母大写: '.Ucfirst($s); echo '<br />'; echo '每个单词首字母大写: '.Ucwords($s); echo '<br />'; echo '大写: '. Strtoupper($s); echo '<br />'; echo '小写: '. Strtolower($s); echo '<br />'; // 转义字符串函数,存入数据库前使用 echo 'addslashes(); stripslashes();'; $s1 = '"a"b'; echo '<br />'; echo addslashes($s1); echo '<br />'; //截取 echo substr($s, 4, 4); echo '<br />'; $s2 = "hello world"; $s3 = "test"; //比较,相等返回0 echo strcmp($s, $s2) == 0 ? "相等":"不等"; echo '<br />'; echo strcmp($s, $s3) == 0 ? "相等":"不等"; echo '<br />'; //查找 echo strpos($s, 'o'); echo '<br />'; echo strrpos($s, 'o'); //注意:如果查不到,则返回false == 0,所以要用"==="来检查if (XX === false); echo '<br />'; //反转 echo strrev($s); echo '<br />'; //切割 $arr = str_split($s); $arr1 = str_split($s, 2); $arr2 = explode(' ', $s); var_dump($arr); var_dump($arr1); var_dump($arr2); ?>
I hope this article will be helpful to everyone in PHP programming design.
For more usage examples of common PHP string processing functions [conversion, escaping, interception, comparison, search, reversal, cutting], please pay attention to the PHP Chinese website for related articles!