使用PHP中的正则表达式进行字符串匹配

2023-05-17 10:16:20

本文主要描述了如何使用PHP中的正则表达式进行字符串匹配

$pattern = "/world/i"; // 定义的模式,i表示不区分大小写
$text = "Hello World!"; // 要匹配的字符串

if (preg_match($pattern, $text)) {
echo "找到匹配的字符串";
} else {
echo "未找到匹配的字符串";
}
在PHP中使用PDO进行数据库连接和操作

// 连接数据库
$dsn = "mysql:host=localhost;dbname=test";
$username = "root";
$password = "root";
$dbh = new PDO($dsn, $username, $password);

// 查询
$sql = "SELECT * FROM users WHERE id=:id";
$sth = $dbh->prepare($sql);
$sth->bindParam(":id", $id);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);

// 插入
$sql = "INSERT INTO users (name, age) VALUES (:name, :age)";
$sth = $dbh->prepare($sql);
$sth->bindParam(":name", $name);
$sth->bindParam(":age", $age);
$sth->execute();
使用PHP中的Session进行用户会话管理

// 开启Session
session_start();

// 设置Session变量
$_SESSION['username'] = 'test';

// 获取Session变量
$username = $_SESSION['username'];

// 清除Session变量
unset($_SESSION['username']);

// 销毁Session
session_destroy();
  • 作者:
  • 原文链接:
    更新时间:2023-05-17 10:16:20