2017-09-13 67 views
0

在我的代码中,我试图从href的onclick中提取确切的值我搜索了各种帖子,但没有成功,我找到了一个解决方案,但尝试了几次后,我做了一个烂摊子我来自onclick函数的PHP提取值

这里是我的榜样下载串

<a href="#" onclick="download_file('tokenid','n','hashname')">Get Name</a> 

我的要求是这种格式只提取值,这样我就可以使用爆炸功能seprate

tokenid,n,hashname 

目前我的PHP代码是这样的

$onclickAttr = $printtable; 
if(preg_match("~download_file\('\K[^']++~", $onclickAttr, $match)) 
$result[] = $match[0]; 
var_dump($result); 
+0

哪里是所有这些细节?你可以不使用'$ _GET'或AJAX将它们传递给PHP吗? – Script47

+0

@ Script47他是在php中literaly解析html内容 – bluehipy

回答

1

这里我们使用preg_matchstr_replace,以实现所需的输出。

正则表达式:download_file\(\K[^)]+

download_file\(\K这将匹配download_file(\K将重置当前的匹配。

2.[^)]+这将匹配所有除)

Try this code snippet here

preg_match("#download_file\(\K[^)]+#", $string, $matches); 
echo str_replace("'", "", $matches[0]); 
+1

Sahil完美无缺的工作感谢 – Rtra

+0

@Rtra欢迎你的朋友高兴地帮助你... :) –

0

问题是你的正则表达式。你的表达必须匹配括号之间的所有内容。此外,preg_match响应中的第一个元素始终是输入字符串,因此请使用正确的索引来检索实际匹配(在我的解决方案中为1)。

尝试修改,就像波纹管。

preg_match('/download_file\(([^\)]+)\)/', $onclickAttr, $match); 
$result[] = $match[1]; 
var_dump($result); 
+0

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 - [来自评论](/ review/low-quality-posts/17318818) – Jerodev

+0

工作一点,但它仍然显示'download_file('value','value','value')'而不是'value,value,value' – Rtra

+0

@Rtra是否像我上面写的那样使用$ match [0]或$ match [1]? – bluehipy

0

你应该使用AJAX值发送到你的PHP脚本

0
you can get all **argument in array** can do any operation on array element 

function download_file(){ 
     var htmlOutput = []; 
     for (var i=0; i < arguments.length; i++) { 
      htmlOutput.push(arguments[i]); 
     } 
     console.log(htmlOutput); 
    }