2017-04-22 128 views
-4

我需要在php文件中获取javascript变量值。在php中获取javascript变量值

HTML例如: UPDATE:

$html = ' 
<script> 
window.runParams.adminSeq="3423423423423"; 
window.runParams.companyId="2349093284234"; 
</script>'; 

喊我用正则表达式?正则表达式对我来说非常复杂...任何帮助?

+2

为什么不把它传递给GET变量? – Rahul

+0

AJAX请求或带查询字符串参数的简单重定向听起来很熟悉? –

+1

我同意@Rahul –

回答

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"]; 
?> 
+0

谢谢Marius的回复,但我需要知道adminSeq有这样的代码:3423423423423.是否有可能搜索一个词并获取“”中的值。我试图使用正则表达式:[[:word:]] + \ d但它获得所有数字... – andys

+0

或者这样做:获取包含单词“adminSeq”的代码行并获取“”之间的值。但我不知道如何写在正则表达式... – andys

+0

代码更新,现在包括变量名称,添加foreach循环,所以你声明你的变量,然后foreach变量它获得的价值。我希望这有助于:) –