发布于 2023-11-12 14:02:27 浏览 326 次
// 假设 $text 是包含 JSON 数据的字符串
$text = 'Some text before the JSON data {"name": "John", "age": 30} Some text after the JSON data';
// 使用正则表达式匹配整段 JSON 数据
$pattern = '/\{(?:[^{}]|(?R))*\}/';
if (preg_match($pattern, $text, $matches)) {
$json_data = $matches[0];
// 现在 $json_data 包含了整段 JSON 数据
echo $json_data;
} else {
echo "未找到匹配的 JSON 数据。";
}