本文列举了 PHP 字符串相关函数。
替换
1 | mixed str_replace( mixed $search, mixed $replace , mixed $subject [, int &$count]); |
$count
如果被指定,它的值将被设置为替换发生的次数。
上述函数区分大小写,str_ireplace()
不区分大小写。
特殊字符相关函数
增加反斜线
以 C 语言风格使用反斜线转义字符串中的字符
1 | string addcslashes ( string $str , string $charlist ) |
使用反斜线引用字符串 单引号(’)、双引号(”)、反斜线(\)与 NUL
1 | string addslashes ( string $str ) |
去掉反斜线
1 | string stripcslashes ( string $str ) |
\'
转换为 '
等等
1 | string stripslashes ( string $str ) |
单引号
想要输出 \
,须输入 echo '\\'
。
echo '\r';
等会原样输出。
双引号
能够解析特殊字符
1 | echo "$a"; |
heredoc
1 | <<<EOF |
nowdoc
1 | <<<'EOF' |
生成随机字符串
1 | /** |
uniqid()
可能有重复数据
其他
首字母大写/小写
ucfirst()
lcfirst()
字符串中每个单词首字母大写
ucwords('a b c')