我需要在php文件中获取javascript变量值。
html示例: 的更新:
$html = '
<script>
window.runParams.adminSeq="3423423423423";
window.runParams.companyId="2349093284234";
</script>';
喊我用正则表达式?正则表达式对我来说非常复杂......任何帮助?
答案 0 :(得分:3)
<?php
$html = '<script>
window.runParams.adminSeq="3423423423423";
window.runParams.companyId="2349093284234";
</script>';
$variables = ["adminSeq", "companyId"];
$counter = 0;
foreach($variables as $variable) {
preg_match_all('/"(.*?)"/', $html, $matches);
${"$variable"} = ($matches[1])[$counter];
$counter++;
}
echo $adminSeq; // Prints out: 3423423423423
echo $companyId; // Prints out: 2349093284234
?>
您也可以使用GET请求执行此操作。该链接看起来像http://localhost/?adminSeq=3423423423423&companyId=2349093284234
然后在PHP中使用以下内容获取这些值:
<?php
$adminSeq = $_GET["adminSeq"];
$companyId = $_GET["companyId"];
?>