限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >PHP笔记 >PHP基础

PHP函数-字符串函数

分类:PHP基础时间:2017-11-18浏览:4805

去空格或其他字符

trim

名称描述输入输出
trim()删除字符串两端的空格或其他预定义的字符目标字符清除后的字符
$str ="\r\nHello World!\r\n";
echo trim($str);

rtrim(别名chop())

名称描述输入输出
rtrim()删除字符串右边的空格或其他预定义的字符目标字符

ltrim

名称描述输入输出
ltrim()删除字符串左边的空格或其他预定义的字符目标字符

dirname

名称描述输入输出
dirname()返回路径中的目录部分一个包含路径的字符串返回文件路径的目录部分
例:
echo dirname("f://www/a/b/1.php");
//返回目录  f://www/a/b

字符串生成与转换

str_pad

函数名描述输入输出
str_pad(字符串变量,新的长度,"填充符号")把字符串填充为指定长度/要填充的字符串/新字符串的长度/供填充使用的字符串,默认是空白完成后的字符串
例:
$str = "hahaha";
echo str_pad($str,10,"@")
//输出  hahaha@@@@

str_report

函数名描述输入输出
str_report("指定字符",重复的次数)重复使用指定的字符串
例:
echo str_repeat("$",5);
//返回 $$$$$

str_split

函数名描述输入输出
str_splite()把字符串分割到数组中
例:
print_r(str_split("Hello"));
//返回Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )

strrev

函数名描述输入输出
strrev()反转字符串目标字符串反转后的字符串
例:
echo strrev("hahaha");
//返回  ahahah

wordwrap

函数名描述输入输出
wordwrap(变量,长度)按指定长度对字符串进行折行处理目标字符串/最大宽度折行后的字符串

str_shuffle()

函数名描述输入输出
str_shuffle()随机的打乱字符串的所有字符

parse_str()

函数名描述输入输出
parse_str("字符串",$数组名)将字符串解析成变量
parse_str("id=23&name=John%20Adams",$myArray);
print_r($myArray);
//Array ( [id] => 23 [name] => John Adams )

number_format()

函数名描述输入输出
number_format(数字)通过千位分组格式化数字
例:
echo number_format(222222222);
//222,222,222

大小写转换

strtolower()

函数名描述输入输出
strtolower("字符串")字符串转换成小写

strtoupper()

函数名描述输入输出
strtoupper("字符串")字符串转换成大写

ucfirst()

函数名描述输入输出
ucfirst(字符串")字符串首字母大写

ucwords()

函数名描述输入输出
ucwords("字符串")字符串中每个首字母都大写

html标签关联

htmlentities()

函数名描述输入输出
htmlentities($str, ENT_COMPAT)把字符转为HTML实体
$str ="John & 'Adams'";
echo htmlentities($str, ENT_COMPAT);

htmlspecialchars()

函数名描述输入输出
.预定义字符转html编码

nl2br()

函数名描述输入输出
nl2br("字符串中有\n")\n转义为
标签
echo nl2br("ds\ndsa");//\n转换为换行

strip_tags()

函数名描述输入输出
strip_tags("字符串和各类标签")剥去 HTML、XML 以及 PHP 的标签
echo strip_tags("
dsdsa\n");//dsdsa

addcslashes()

函数名描述输入输出
addcslashes(变量,'指定字母')在指定的字符前添加反斜线转义字符串中字符
$str = "Hello, my name is John Adams.";
echo $str;
echo "
"; echo addcslashes($str,'m'); //Hello, my name is John Adams. //Hello, \my na\me is John Ada\ms.

stripcslashes()

函数名描述输入输出
stripcslashes(含\的字符串)删除由addcslashes()添加的反斜线

addslashes()

函数名描述输入输出
addslasher(变量)指定预定义字符前添加反斜线
例
$str = "Who's John Adams?";
echo addslashes($str);
//Who\'s John Adams?

stripslashes()

函数名描述输入输出
stripslashes(变量)删除由addslashes()添加的转义字符
例
$str = "Who\'s John Adams?";
echo stripcslashes($str);
//Who's John Adams?

quotemeta()

函数名描述输入输出
quotemeta()在字符串中某些预定义的字符前添加反斜线
例
$str = "Hello world. (can you hear me?)";
echo quotemeta($str);
//Hello world\. \(can you hear me\?\

chr()

函数名描述输入输出
chr()从指定的 ASCII 值返回字符
echo chr(052);//*

ord()

函数名描述输入输出
ord()返回字符串第一个字符的 ASCII 值
echo ord("hello");//104

字符串比较

strcasecmp()

函数名描述输入输出
strcasecmp(字符串,字符串)不区分大小写比较两字符串大1/等0/小-1
echo strcasecmp("Hello world!","HELLO WORLD!");
//0

strcmp()

函数名描述输入输出
strcmp()区分大小写比较两字符串

strncmp()

函数名描述输入输出
比较字符串前n个字符,区分大小写
int strncmp ( string $str1 , string $str2 , int $len )

strncasecmp()

函数名描述输入输出
.比较字符串前n个字符,不区分大小写

strnatcmp()

函数名描述输入输出
.自然顺序法比较字符串长度,区分大小写
int strnatcmp ( string $str1 , string $str2 )

strnatcasecmp()

函数名描述输入输出
.自然顺序法比较字符串长度,不区分大小写
int strnatcasecmp ( string $str1 , string $str2 )

字符串切割与拼接

chunk_split()

函数名描述输入输出
.将字符串分成小块
str chunk_split(str $body[,int $len[,str $end]])

strtok()

函数名描述输入输出
.切开字符串
str strtok(str $str,str $token)

explode()

函数名描述输入输出
.使用一个字符串为标志分割另一个字符串
array explode(str $sep,str $str[,int $limit])

implode()

函数名描述输入输出
.同join,将数组值用预订字符连接成字符串
string implode ( string $glue , array $pieces )

substr()

函数名描述输入输出
.截取字符串
string substr ( string $string , int $start [, int $length ] )

字符串查找替换

str_replace()

函数名描述输入输出
.字符串替换,区分大小写'查找的字符串','替换的字符串','主字符串-(查找的字符串)')替换后的字符串
echo str_replace('查找的字符串','替换的字符串','主字符串-(查找的字符串)');//替换字符串

str_ireplace()

函数名描述输入输出
.替换字符串,不区分大小写
与上个相反

substr_count()

函数名描述输入输出
.统计一个字符串在另一个字符串出现的次数主字符串,要查找的字符串出现的次数
echo substr_count('aaaaaaa','a');//7

substr_replace()

函数名描述输入输出
substr_replace('abcdefg','123',1,4);字符串替换主字符串,替换字符串,开始下标,[结束下标]新的字符串
echo substr_replace('abcdefg','123',1);//a123
echo substr_replace('abcdefg','123',1,4);//a123gf

similar_text()

函数名描述输入输出
similar_text('abcde','abc')返回两字符串相同字符的数量字符串,字符串相同字符的数量
echo similar_text('abcde','abc');//3

strrchr() == strchr()

函数名描述输入输出
strrchr('abc123abc456','abc')输出最后一次出现到末尾主字符串,查找的字符串最后一次出现的查找字符及后面字符
echo strrchr('abc123abc456','abc');//abc456

strstr()

函数名描述输入输出
strstr('abc123abc456','abc')返回第一次出现到末尾主字符串,查找的字符串第一次出现的查找字符及后面字符
echo strstr('abc123abc456','abc');//abc123abc456

stristr()

函数名描述输入输出
stristr('11abc123abc456','abc')第一次出现到末尾,并不区分大小写主字符串,查找的字符串第一次出现的查找字符及后面字符
echo stristr('11abc123abc456','abc');//abc123abc456

strtr()

函数名描述输入输出
strtr('11abc123abc456','abc','bcd')转换字符串中指定字符主字符串,替换字符串,替换为的字符串新的字符串
echo strtr('11abc123abc456','abc','bcd');//11bcd123bcd456

strpos()

函数名描述输入输出
strpos('11abc123abc456','abc')寻找字符串中某字符串出现的最先位置 ,区分大小写主字符串,要查找的字符串输出位置
echo strpos('11abc123abc456','abc');//2 

stripos()

函数名描述输入输出
.寻找字符串中某字符串出现的最先位置 ,不区分大小写
同上

strrpos()

函数名描述输入输出
.寻找某字符串出现的位置,区分大小写
同上

strripos()

函数名描述输入输出
.寻找某字符串出现的位置,不区分大小写

strspn()

函数名描述输入输出
.返回字符串中首次符合mask的子字符串长度

strcspn()

函数名描述输入输出
.返回字符串中首次不符合mask的子字符串长度

字符串统计

str_word_count()

函数名描述输入输出
str_word_count(字符串)统计字符串含有的单词数目标字符串个数
echo str_word_count('acsa sas');//2  统计字符串含有的单词数

strlen()

函数名描述输入输出
strlen(字符串)统计字符串长度字符串长度

count_chars()

函数名描述输入输出
count_chars(字符串)统计字符串中所有字母出现的次数字符串下标为0-255的ASCII,值为出现次数,组成的数组
var_dump(count_chars('sdasdsagf'));

字符串编码

md5()

函数名描述输入输出
md5()字符串md5编码
"$str = ""Hello"";
echo md5($str);
"

json_encode()

函数名描述输入输出
json_encode($arr);把数组转化成json格式返回json

json_decode()

函数名描述输入输出
json_decode($arr,true);把json转化成数组true参数是返回数组,不加返回对象返回数组

ini_get

函数名描述输入输出
.获取一个配置php-ini选项的值
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.