0

Hi I am trying to get exact value from javascript onclick.

Here is my example link:

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;"

here is what I am trying:

preg_match_all(
    "~onclick\s*=\s*([\"\'])(.*?)\\1~si", $d_l, $match);
$link = $match[0][0];

I am getting full onclick not the exact value, I want to get link as output:

(
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)

Can any one help please?

Ian Kenney
  • 6,376
  • 1
  • 25
  • 44
Harinder
  • 1,257
  • 8
  • 27
  • 54
  • 3
    Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – bwoebi Jun 22 '13 at 19:41
  • What is in the second array[0] element? `$link = $match[0][1];`. But I agree with bwoebi - use a parser. – Andy G Jun 22 '13 at 19:52
  • i am using Simple Html Dom but cant get exact value from onclick ... so i was using preg_match_all... – Harinder Jun 22 '13 at 19:57

2 Answers2

4

An example on how you can do this properly:

<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);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

$match[0][$i-1] is the whole $i-th match, $match[1][$i-1] corresponds to the first submatch in the $i-th match, etc.

To get just the links, try this:

preg_match_all(
    "~onclick\s*=\s*([\"\']).*?downloadFile\(([\"'])(.*?)\\2.*?\).*?\\1~si",
    $d_l, $match
);
foreach ($matches[3] as $link)
    echo $link, "<br>\n";
Vedran Šego
  • 3,553
  • 3
  • 27
  • 40