int strlen(string str)
strlen()会对空格和0计算长度,在预期出现 0或空格的 if 判断中使用 strlen()判断非空会更加精准
$pwd = 'qq123456';
$str = ' 0';
echo strlen($pwd);
echo strlen($str);
输出: 8 , 2
strtolower() 字符串全部转小写
strtoupper() 字符串全部转大写
ucfirst() 字符串第一个字母转大写
ucwords() 字符串所有单词首字符转大写
$str = 'The commonest thing is delightful if only one hides it.';
echo strtolower($str) ,"\n";
echo strtoupper($str) ,"\n";
echo ucfirst($str),"\n";
echo ucwords($str) ,"\n";
输出:
the commonest thing is delightful if only one hides it.
THE COMMONEST THING IS DELIGHTFUL IF ONLY ONE HIDES IT.
The commonest thing is delightful if only one hides it.
The Commonest Thing Is Delightful If Only One Hides It.
删除html标签,
$html = "首页: 点击链接 answer
";
echo strip_tags($html);
// echo strip_tags($html, ''); // 不删除a标签
输出:首页: 点击链接 answer
int strpos(string str, string substr [, int offset])
参数1字符串,参数2 指定搜索的字符, 参数3从哪开始搜索
stripos() 相同的功能,只是不区别大小写
strpos ('abcdefg', 'ef');
输出: 4
echo strrpos ('1001100200100', '100');
输出: 10
参数一目标值,参数二替换值,参数三执行替换的数组或者字符串
str_ireplace() 不区分大小写
$email = str_replace("@", "(at)", "[email protected]");
$script = str_replace(['<','>','.','/'], ['(',')',',','\/'], "'http://www.abc.com ");
$array = str_replace('.', ',', ['http://www.b.js', 'www.google.js']);
输出:
500000(at)qq.com
(abc)http:\/\/www,abc,com(\/abc)
Array ( [0] => http://www,b,js [1] => www,google,js )
string strstr( string str, string occurrence [ , bool before_needle])
参数3为true, 返回截取 occurrence 之前的字符串
有点像explode() 函数
echo strstr("[email protected]", "@");
echo strstr("[email protected]", "@", true);
输出: @gmail.com, sales
使用另一个字符串填充字符串为指定长度
str_pad(“abc”, 10);原字符串已有3个字符,将会填充7个空格。
参数三为填充字符串
echo strlen(str_pad("Salkad", 10) . "is good");
echo str_pad("Log Report", 20, "+=" ,STR_PAD_BOTH);
输出:
17
+=+=+Log Report+=+=+
string trim(string str [ , string charlist])
默认删除两边空格,有参数时删除指定字符,包括空格,水平制表符(\t),换行(\n), 回车(\r),空值(\o),垂直制表符(\xob)
ltrim() 删除左边字符
rtrim() 删除右边字符
$str = ",,,lg report...[[[";
echo trim($str, ",.[");
输出:log report
返回字符串中单词的使用情况
str_word_count ( string $string [, int $format = 0 ] )
format 0 - 返回单词数量, 1 - 返回一个包含 string 中全部单词的数组
$str = "Hello fri3nd, you're
looking . good today!";
$count = str_word_count($str, 0);
$words = str_word_count($str, 1);
输出: 7, Array ( [0] => Hello [1] => fri [2] => nd [3] => you’re [4] => looking [5] => good [6] => today)
filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] ) : mixed