Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scraping Javascript variables to PHP

I'm trying to parse some HTML with Xpath but am finding out the the links I want to get are generated by some javascript and not using just a normal a anchor. The javascript is as follows:

<script type="text/javascript">
    var Hyperurl="ab5";
    var Hyperlink="46439157";
</script>

Now, I've used XPath to grab the script code via:

$xpath->query('//script[contains(.,"Hyper")]');

Which returns:

var Hyperurl="ab5";var Hyperlink="46439157";

My question is. How do I get this data into an array much like parse_url or the like? Should I just preg_match_all the variable storing the string? If so, what regex would I use? Or is there a better way to parse and grab the data I want?

Thanks in advance!

like image 266
tr3online Avatar asked Aug 12 '11 20:08

tr3online


2 Answers

You could try:

preg_match_all('/"(.*?)"/', $variables, $array);

I think your variables would then be $array[1] and $array[2].

like image 74
bozdoz Avatar answered Nov 08 '22 20:11

bozdoz


You could use this

preg_match_all('/var\s+(\w+)\s*=\s*(["\']?)(.*?)\2;/i', $js, $matches);

$matches[1] will contain the variable names, and $matches[3] will contain their values.

like image 21
Flambino Avatar answered Nov 08 '22 19:11

Flambino