![](/img/trans.png)
[英]regular expression to extract a query string parameter value from a URL in PHP
[英]Regular expression to extract the onclick value from a string
嗨,我正試圖從javascript onclick獲得確切的價值。
這是我的示例鏈接:
onclick="omniture('Touchpad_8.0.7.2.ZIP','NP-N150P');downloadFile('http://xxx.com/downloadfile/ContentsFile.aspx?CDSite=UNI_CO&CttFileID=3017288&CDCttType=DR&ModelType=N&ModelName=NP-N150P&VPath=DR/201105/20110509115437867/Touchpad_8.0.7.2.ZIP','ZIP');return false;"
Lan o red inalambrica BROADCOM - 5.100.82.95 - onclick="omniture('WLAN_Broadcom_5.100.82.95.ZIP','NP-N150P');downloadFile('http://xxx.com/downloadfile/ContentsFile.aspx?CDSite=UNI_CO&CttFileID=3017290&CDCttType=DR&ModelType=N&ModelName=NP-N150P&VPath=DR/201108/20110817201634927/WLAN_Broadcom_5.100.82.95.ZIP','ZIP');return false;"
這是我正在嘗試的:
preg_match_all(
"~onclick\s*=\s*([\"\'])(.*?)\\1~si", $d_l, $match);
$link = $match[0][0];
我正在獲得完整的onclick而不是確切的值,我想獲取鏈接作為輸出:
(
http://xxx.com/downloadfile/ContentsFile.aspx?CDSite=UNI_CO&CttFileID=3017290&CDCttType=DR&ModelType=N&ModelName=NP-N150P&VPath=DR/201108/20110817201634927/WLAN_Broadcom_5.100.82.95.ZIP)
有人可以幫忙嗎?
有關如何正確執行此操作的示例:
<pre><?php
$html = <<<LOD
<html><head></head><body>
<table>
<thead></thead>
<tbody id="tbodyDR">
<tr><td>bidule
<a href="#" onclick="dothis('abcd','1234');downloadFile('http://example.com/bidule.ZiP','ZiP');return false;">bidule</a>
</td></tr>
<tr><td>truc
<a href="#" onclick="dothis('abcd','1234');downloadFile('http://example.com/truc.zIP','zIP');return false;">truc</a>
</td></tr>
<tr><td>bidule
<a href="#" onclick="dothis('abcd','1234');downloadFile('http://example.com/machin.zIp','zIp');return false;">machin</a>
</td></tr>
</tbody>
</body></html>
LOD;
$doc = new DOMDocument();
//@$doc->loadHTMLFile('http://example.com/list.html');
@$doc->loadHTML($html);
$links = $doc->getElementById('tbodyDR')->getElementsByTagName("a");
foreach($links as $link) {
$onclickAttr = $link->getAttribute('onclick');
if( preg_match("~downloadFile\('\K[^']++~", $onclickAttr, $match) )
$result[] = $match[0];
}
print_r($result);
$match[0][$i-1]
是整個$i
-th匹配項, $match[1][$i-1]
對應於$i
-th匹配項中的第一個子匹配項,依此類推。
要獲取鏈接,請嘗試以下操作:
preg_match_all(
"~onclick\s*=\s*([\"\']).*?downloadFile\(([\"'])(.*?)\\2.*?\).*?\\1~si",
$d_l, $match
);
foreach ($matches[3] as $link)
echo $link, "<br>\n";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.