将字符串转换为大写字母。
PHP常用字符串String函数实例总结
转换函数
strtoupper()函数
将字符串转换为大写字母。
<?php
$str = "hello world!";
echo strtoupper($str); // 输出HELLO WORLD!
?>
strtolower()函数
将字符串转换为小写字母。
<?php
$str = "HELLO WORLD!";
echo strtolower($str); // 输出hello world!
?>
ucfirst()函数
将字符串的首字母转换为大写字母。
<?php
$str = "hello world!";
echo ucfirst($str); // 输出Hello world!
?>
ucwords()函数
将字符串中每个单词的首字母转换为大写字母。
<?php
$str = "hello world!";
echo ucwords($str); // 输出Hello World!
?>
替换函数
str_replace()函数
在字符串中查找并替换内容。
<?php
$str = "hello world!";
echo str_replace("world", "mars", $str); // 输出hello mars!
?>
preg_replace()函数
使用正则表达式在字符串中查找并替换内容。
<?php
$str = "hello world!";
echo preg_replace("/world/i", "mars", $str); // 输出hello mars!
?>
计算函数
strlen()函数
获取字符串的长度。
<?php
$str = "hello world!";
echo strlen($str); // 输出12
?>
substr_count()函数
统计子字符串在字符串中出现的次数。
<?php
$str = "Hello world. The world is nice.";
echo substr_count($str, "world"); // 输出2
?>
strpos()函数
查找字符串中第一次出现的位置,返回位置的索引值(从0开始)。
<?php
$str = "hello world!";
echo strpos($str, "world"); // 输出6
?>
截取函数
substr()函数
截取字符串的一部分。
<?php
$str = "hello world!";
echo substr($str, 0, 5); // 输出hello
?>
explode()函数
将字符串分割成数组。
<?php
$str = "hello,world!";
$arr = explode(",", $str);
print_r($arr); // 输出Array([0] => hello [1] => world!)
?>
加密函数
md5()函数
对字符串进行MD5加密。
<?php
$str = "hello world!";
echo md5($str); // 输出5eb63bbbe01eeed093cb22bb8f5acdc3
?>
sha1()函数
对字符串进行SHA1加密。
<?php
$str = "hello world!";
echo sha1($str); // 输出2ef7bde608ce5404e97d5f042f95f89f1c232871
?>
织梦狗教程
本文标题为:php常用字符串String函数实例总结【转换,替换,计算,截取,加密】


基础教程推荐
猜你喜欢
- laravel5.6中的外键约束示例 2023-03-08
- PHP设计模式之工厂模式定义与用法详解 2022-10-12
- PHP 生成器的使用详解 2023-06-03
- Laravel 6 将新增为指定队列任务设置中间件的功能 2023-01-31
- PHP获取文件属性的最简单方法 2022-09-02
- 关于PHP隐藏入口文件问题 2022-10-09
- php防止表单重复提交实例讲解 2022-12-18
- PHP方法的返回值示例详解 2023-05-09
- 详解PHP框架EasySwoole 2022-09-01
- thinkphp中U方法按路由规则生成url的方法 2022-10-08