Usage examples of common string processing functions in PHP [conversion, escaping, interception, comparison, search, reversal, cutting]

高洛峰
Release: 2023-03-05 08:36:01
Original
1019 people have browsed it

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); ?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!