Home  >  Article  >  Backend Development  >  Some common string functions in PHP

Some common string functions in PHP

不言
不言Original
2018-04-03 13:48:551447browse

本篇文章介绍的是php中的一些常用字符串函数,在这里分享给大家,也可以给有需要的朋友一点参考,大家一起来看看吧

1、获取字符串长度 strlen()


2、删除空白字符串 trim(), ltrim(), rtrim() 分别用来删除 字符串首尾,字符串开头,字符串结尾的空白符。

trim(),ltrim(),rtrim()函数默认删除的字符:

符号 ASCII码 意义
“ ” 0x20 空格
“\t” 0x09 制表符
"\n" 0X0A 换行符
"\r" 0x0D 回车符
"\0" 0x00 空字符
"\x0B" 0x0B 纵向制表符


//examgle
$title = "  Programming PHP  \n";
$str1 = ltrim($title);//Promgromming PHP \n
$str2 = rtrim($title);//  Programming PHP
$str3 = trim($title);//Programming PHP
//可以使用带参数的trim()函数来删除开头或结尾的空白而不删除制表符
$record = " Fred\tFlintstone\t35\tWilma\t  \n";
$record = trim($record, " \r\n\0\x0B");
//此时record 值为:"Fred\tFlintstone\t35\tWilma\t"

改变大小写

strtolower(),strtoupper() 操作整个字符串

ucfirst()仅操作字符串的第一个字符,ucwords()操作字符串中每个单词的第一个字符

//example
$str1 = "FRED fintstone";
$str2 = "barney rubble" ;
print(strtolower($str1));
print(strtoupper($str1));
print(ucfirst($str2));
print(ucwords($str2));
//输出
fred fintstone
FRED FINTSTONE
Barney rubble
Barney Rubble

相关推荐:

php 字符串函数总结

PHP常用数组和字符串函数总结

The above is the detailed content of Some common string functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:oauth2 practice in phpNext article:oauth2 practice in php