PHP中常用字符串函数

// strlen 取字符串长度
$string = "Hello, world!";
$length = strlen($string);
echo $length; // 输出:13


// strpos 找字符串,返回位置
$str = "hello world";
$position = strpos($str,"world");
echo $position; // 输出:7


// substr 截取字符串
$str = "hello world";
$substring = substr($str,7,5); // 从第7位开始 截取5个字符
echo $substring; // 输出:world


// str_replace 替换字符串
$str = "hello world";
$newStr = str_replace("world","php",$str);
echo $newStr; // 输出:hello php


// strtolower 字符串转小写
$str = "Hello WORLD";
$lowStr = strtolower($str);
echo $str; // 输出:hello world


// strtoupper 字符串转大写
$str = "hello world";
$upStr = strtoupper($str);
echo $upStr; // 输出:HELLO WORLD


// trim 去首尾空或指定字符
$str = " hello world ";
$newStr = trim($str);
echo $newStr; // 输出:hello world

 

posted @ 2023-06-18 12:59  波罗斯の程序日记  阅读(3)  评论(0编辑  收藏  举报