You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP代码报Parse error语法错误:提取hidden字段name值失败求助

PHP正则匹配出现语法错误的原因及修复方案

首先,咱们来看你遇到的Parse error: syntax error, unexpected 'hidden' (T_STRING)错误,这个问题的核心是字符串包裹符的冲突导致PHP解析器无法正确识别正则表达式的边界。

错误根源分析

你写的第10行代码:

preg_match("/<input type=\"hidden\" name=\"(.*?)\" value=\"1\"/", $result, $matches);

理论上,用双引号包裹正则并转义内部双引号是可行的,但如果你的代码里转义反斜杠意外丢失(比如复制粘贴、编辑器自动转义失效等),就会变成这样:

preg_match("/<input type="hidden" name="(.*?)" value="1"/", $result, $matches);

这时候,type="里的第二个双引号会直接闭合外层的字符串,后面的hidden就成了一个脱离字符串的裸标识符,PHP解析器完全不知道它是什么,于是抛出了语法错误。

修复方案

最稳妥的方式是改用单引号包裹正则表达式,这样内部的双引号不需要任何转义,彻底避免冲突:

preg_match('/<input type="hidden" name="(.*?)" value="1"/', $result, $matches);

如果坚持用双引号包裹,一定要确保所有内部双引号都正确添加转义反斜杠:

preg_match("/<input type=\"hidden\" name=\"(.*?)\" value=\"1\"/", $result, $matches);

额外优化提示

你的curl代码里有重复的curl_exec($ch);调用,这会导致发起两次HTTP请求,完全没必要,删掉其中一次即可:

$url = "http://www.ugodambrosi.it/index.php?option=com_users&view=registration";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// 删掉下面这行重复的执行
// curl_exec($ch);
curl_close($ch);
preg_match('/<input type="hidden" name="(.*?)" value="1"/', $result, $matches);
print_r($matches);

内容的提问来源于stack exchange,提问作者yudi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:25:09