php字符串常用,PHP常用的字符串操作

e94ba92e5fcc

HA.jpg

$str = 'tihuanzhexie';

$str1 = str_replace('chazhao','tihuan',$str); // preg_replace();// 字符串替换

$str19 = substr_replace('13826505722','****',3,4); // 138****5722

$arr = explode(',',$str);// 字符串转化成数组

$str2 = implode(',',$arr); //"tihuanzhexie",// 数组转成字符串 别名 join

$str3 = substr($str2,3,'4');// substr,从str第3位开始截取长度4"uanz"

$str4 = substr($str2,-5); //从倒数第5位开始截取hexie

$str5 = substr($str2,-5,3);//从倒数第5位开始截取3位 hex

$str6 = strstr("明日编程词典","编");//“编程词典” 获取一个字符串在另一字符串中首次出现的位置 到后者末尾的字符串,

//如果执行成功,则返回剩余字符串(存在相匹配的字符串),否则返回false。 别名strchr()

$str7 = strpos('我是谁我在哪','在');// 12 中文字符编码

$str8 = strpos('Iamaworker','w');// 4 获取查找的字符串在另一字符串中首次出现的位置。

//该函数对大小写敏感,如需进行对大小写不敏感的搜索,请使用 stripos() 函数

$str9 = ucwords('asc desc order'); // 字符串每个单词首字母转大写 Asc Desc Order

$str10 = ucfirst('asc desc'); // 字符串首字母转大写 Asc desc

$str11 = strtoupper('asc desc'); //字符串转大写 ASC DESC

$str12 = strtolower("ASC DESC"); // 字符串转小写 asc desc

$str13 = lcfirst('ASC DESC'); // 首字符转小写 aSC DESC

$str14 = 'abc'.''.'def'; //运算符 (.) 用于把两个字符串值连接起来

$str15 = strlen('abcde'); // 5 获取字符串的长度

$str16 = md5('abc123'); // 字符串MD5的散列

$str17 = trim(" abc "); //移除字符串中的空白字符

$str18 = trim('abcDe','De'); //移除字符串中的De ,ltrim(),rtrim()

$str20 = substr_count('abcdeabcab','ab'); // 3 子字符串在字符串中出现的次数

$str21 = str_shuffle('abcde'); // ebcda 随机打乱字符串

$str22 = str_split('abcdef',3); // ["abc","def"] 按指定的元素长度分割字符串成数组,默认元素长度为1

$str23 = str_split('abcdef'); // ["a","b","c","d","e","f"]

$str24 = strrev('abc'); //cba 翻转字符串

$str25 = crypt('abc','123'); //"12BWKETBcM70Q" 单向的字符串加密法(hashing)

rand(1000,9999)4位的随机字符串

后续再有再添加吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP字符串函数可以分为以下几类: 1. 字符串操作函数:用于对字符串进行操作,如拼接、替换、截取等。常用函数有:strlen、substr、str_replace、str_pad等。 示例: 获取字符串长度: ``` $str = "hello world"; $length = strlen($str); // $length = 11 ``` 截取字符串: ``` $str = "hello world"; $sub_str = substr($str, 0, 5); // $sub_str = "hello" ``` 替换字符串: ``` $str = "hello world"; $new_str = str_replace("world", "php", $str); // $new_str = "hello php" ``` 2. 字符串格式化函数:用于对字符串进行格式化,如大小写转换、去除空格等。常用函数有:strtolower、strtoupper、trim等。 示例: 将字符串转换为大写: ``` $str = "hello world"; $new_str = strtoupper($str); // $new_str = "HELLO WORLD" ``` 去除字符串的空格: ``` $str = " hello world "; $new_str = trim($str); // $new_str = "hello world" ``` 3. 字符串查询函数:用于查询字符串是否包含某个子串或者某个字符的位置。常用函数有:strpos、strrpos、strstr等。 示例: 查询字符串是否包含某个子串: ``` $str = "hello world"; $pos = strpos($str, "world"); // $pos = 6 ``` 查询字符在字符串最后一次出现的位置: ``` $str = "hello world"; $pos = strrpos($str, "o"); // $pos = 7 ``` 4. 字符串编码函数:用于对字符串进行编码和解码。常用函数有:urlencode、urldecode、base64_encode、base64_decode等。 示例: 将字符串进行 URL 编码: ``` $str = "hello world"; $new_str = urlencode($str); // $new_str = "hello+world" ``` 将字符串进行 Base64 编码: ``` $str = "hello world"; $new_str = base64_encode($str); // $new_str = "aGVsbG8gd29ybGQ=" ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值