test
test

Reputation: 18200

Grabbing content off webpage that is in script tags

OK, I have this page I want to get the content off.. however the stats are made in JavaScript. Is there ANY way I can get the stats? I tried using PHP get_content thingy...

Here is an example that is in the page I want to get. This <script> is between the <body> tag.

< script > 

na=0;
S=new Array;
S[na]="|Beal|3266561|137|131|1170664|714062|1378742|2375|128|322|"; na++; 
S[na]="|Marine|2446933|165554|125613|1116688|652869|187250|23773|27019|148167|"; na++; 
S[na]="|Krackle1|2306919|342794|440503|372482|238609|442226|146516|177399|146390|"; na++; 
S[na]="|LawyerUpSir|1666817|60579|236847|379476|219395|446057|149787|151306|23370|"; na++; 
S[na]="|IKillToWin|1657426|94695|214229|800157|446579|59618|9132|8861|24155|"; na++; 
S[na]="|Farts|1644623|6885|8790|972072|586678|49249|10558|2838|7553|"; na++; 

< / script >

Upvotes: 1

Views: 230

Answers (2)

Fanis Hatzidakis
Fanis Hatzidakis

Reputation: 5340

I expect you mean you have the page's source by getting it remotely via file_get_contents(). Then you just need to use a regular expression to match all lines in that source code that start with S[na], then explode on | to get them in an array form. That should get you the data in a workable format.

$content = <<<END
<script> 

na=0;
S=new Array;
S[na]="|Beal|3266561|137|131|1170664|714062|1378742|2375|128|322|"; na++; 
S[na]="|Marine|2446933|165554|125613|1116688|652869|187250|23773|27019|148167|"; na++; 
S[na]="|Krackle1|2306919|342794|440503|372482|238609|442226|146516|177399|146390|"; na++; 
S[na]="|LawyerUpSir|1666817|60579|236847|379476|219395|446057|149787|151306|23370|"; na++; 
S[na]="|IKillToWin|1657426|94695|214229|800157|446579|59618|9132|8861|24155|"; na++; 
S[na]="|Farts|1644623|6885|8790|972072|586678|49249|10558|2838|7553|"; na++; 

</script>

...some HTML here..

END;

$matches = array() ;
preg_match_all("/S\[na\]\=\"\|(.*)\"\;\sna\+\+\;/", $content, $matches) ;

$stats = array() ;
if (count($matches) > 0 && is_array($matches[1])) {
    foreach ($matches[1] as $match) {
        $stats[] = $match ;
    }
}

Upvotes: 1

nedk
nedk

Reputation: 663

Have a look at http://ca.php.net/manual/en/function.domxml-open-file.php to open the URL as an XML DOM. You can then parse the XML DOM using http://ca.php.net/manual/en/function.domdocument-get-elements-by-tagname.php to get the <script> tags.

Upvotes: 1

Related Questions