This article analyzes the usage of common string processing functions in PHP. Share it with everyone for your reference, the details are as follows:
'; echo '长度为: '.strlen($s); echo '
'; //大小写 echo '首字母大写: '.Ucfirst($s); echo '
'; echo '每个单词首字母大写: '.Ucwords($s); echo '
'; echo '大写: '. Strtoupper($s); echo '
'; echo '小写: '. Strtolower($s); echo '
'; // 转义字符串函数,存入数据库前使用 echo 'addslashes(); stripslashes();'; $s1 = '"a"b'; echo '
'; echo addslashes($s1); echo '
'; //截取 echo substr($s, 4, 4); echo '
'; $s2 = "hello world"; $s3 = "test"; //比较,相等返回0 echo strcmp($s, $s2) == 0 ? "相等":"不等"; echo '
'; echo strcmp($s, $s3) == 0 ? "相等":"不等"; echo '
'; //查找 echo strpos($s, 'o'); echo '
'; echo strrpos($s, 'o'); //注意:如果查不到,则返回false == 0,所以要用"==="来检查if (XX === false); echo '
'; //反转 echo strrev($s); echo '
'; //切割 $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!