以下是一些常用的PHP公用函数:
strlen():返回字符串的长度。
$str = "Hello World!";
echo strlen($str); // 输出 12
strpos():查找字符串中第一次出现的位置。
$str = "Hello World!";
echo strpos($str, "World"); // 输出 6
strtoupper():将字符串转换为大写。
$str = "Hello World!";
echo strtoupper($str); // 输出 HELLO WORLD!
strtolower():将字符串转换为小写。
$str = "Hello World!";
echo strtolower($str); // 输出 hello world!
explode():使用指定的分隔符将字符串分割为数组。
$str = "Hello,World,!";
$arr = explode(",", $str);
print_r($arr); // 输出 Array ( [0] => Hello [1] => World [2] => ! )
implode():将数组元素连接成字符串。
$arr = array("Hello", "World", "!");
$str = implode(" ", $arr);
echo $str; // 输出 Hello World !
date():格式化日期和时间。
echo date("Y-m-d"); // 输出当前日期,例如 2021-01-01
file_get_contents():将整个文件读入一个字符串。
$content = file_get_contents("example.txt");
echo $content; // 输出文件 example.txt 的内容
file_put_contents():将一个字符串写入文件。
$content = "Hello World!";
file_put_contents("example.txt", $content);
json_encode():将数组或对象转换为JSON字符串。
$arr = array("name" => "John", "age" => 30, "city" => "New York");
echo json_encode($arr); // 输出 {"name":"John","age":30,"city":"New York"}
这只是一些常用的PHP公用函数示例,PHP提供了许多其他功能强大的函数,可以根据具体需求进行查阅。
上一篇:php 关联数组列表
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站