Php Javascript将document.write的内容写入变量

I'm in front of this problem that I can't figure out how to solve properly.

I'm using a script:

<script type="text/javascript" src="http://www.gta4.it/stat/view_stats.js.php?mode=0">  </script>

that has as output a single document.write:

document.write('11');

And so the number is displayed using JavaScript. What I want to do is to get the content of this document.write and write it into a variable, JavaScript or PHP, in another server.

Any idea?

That script is just outputting a small JavaScript snippet - You don't need to actually let it execute in the browser if you want to capture its result in PHP:

// Assuming you have [allow_url_fopen][1] enabled
$js = file_get_contents( 'http://www.gta4.it/stat/view_stats.js.php?mode=0');
$value = trim( str_replace( array( "document.write('", "');"), '', $js));
echo $value; // Outputs 9, the current value of the JS

Demo

Of course, if you're looking for a more robust solution that would still work if the JS snippet changes (introduces whitespace, changes quotation types, etc), you can use a regex to extract the number from the snippet. Something like this should work:

$js = file_get_contents( 'http://www.gta4.it/stat/view_stats.js.php?mode=0');
$value = preg_replace('/^\s*document\.write\(\s*[\'"](\d*)[\'"]\s*\)\s*;\s*$/im', '$1', $js);
echo $value; // Outputs 11, the now current value of the JS

Demo

If, for some reason, you want / need to let it execute in a browser before capturing the value, you can do something like this, although I'm sure it's not standards-compliant and seems pretty hackish.

  1. Wrap the <script> tag in a container, perhaps a <span> or <div>
  2. Use JS to get the contents of the container.
  3. Use AJAX to send the result to a server.

Sample (untested):

<div id="stats_container">
    <script type="text/javascript" src="http://www.gta4.it/stat/view_stats.js.php?mode=0">  </script>
</div>
<script type="text/javascript"> 
    var value = document.getElementById( 'stats_container').innerHTML;
    alert( value); // Here you would use AJAX to send the value to a server
</script>

EDIT: I think the hijack of Brian Nickel is the best solution in the case you want it in JS. If this is not a solution for you, you can always try my solution below. Of course for server side retrieval, you can still use things like file_get_contents.

The only (dirty) thing that comes into my mind is that you use an iframe to load a page that includes the script you mention. You can than load the iframe in your actual page and grab whatver is in the iframe into a variable, like so:

$content = getContentFromIframe("something");

function getContentFromIframe(iFrameId){
    var myFrame = document.getElementById(iFrameId);
    return myFrame.contentWindow.document.body.innerHTML;
}

Of course, after retrieval of the data you can remove the iFrame from your page using some scripting. But I assume you hide it using some styling anyway.

If you want to value to be in a server side language like PHP you can just get the contents of the file by calling the file through file_get_contents for example.

You could do something like this in javascript/html

<span id="getvar" style="display: none;">
    <script type="text/javascript" src="http://www.gta4.it/stat/view_stats.js.php?mode=0">  </script>
</span>
<script type="text/javascript">
var getvar = document.getElementById('getvar');
var stat = parseInt(getvar.innerText || getvar.textContent);
</script>

Like any JavaScript function, you can can hijack document.write and replace it with your own function:

<script>
    var oldWrite = document.write; // Save original function.
    var myValue = "";
    document.write = function(str) {myValue += str;}; // Overwrite function.
</script>
<script type="text/javascript" src="http://www.gta4.it/stat/view_stats.js.php?mode=0">  </script>
<script>
    document.write = oldWrite; // Restore old function.
    alert(myValue);
</script>

You can see it in action here: http://jsfiddle.net/qwef4/