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

Some common string functions in PHP

不言
Release: 2023-03-23 06:50:01
Original
1506 people have browsed it

本篇文章介绍的是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
Copy after login
//可以使用带参数的trim()函数来删除开头或结尾的空白而不删除制表符
$record = " Fred\tFlintstone\t35\tWilma\t  \n";
$record = trim($record, " \r\n\0\x0B");
//此时record 值为:"Fred\tFlintstone\t35\tWilma\t"
Copy after login

改变大小写

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

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

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

相关推荐:

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!

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