views:

79

answers:

1

Hi,

I have problem with varible printing inside javascript.

varible htmlString printing not working: document.write(htmlString)

<?php  $htmlString= htmlspecialchars(file_get_contents('http://google.com'));?&gt;
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <script type="text/javascript">  
      var htmlString="<?php echo $htmlString; ?>";
      document.write(htmlString);
    </script>

  </body>
</html>


Edit:

Webpage source result: - Get all google.com inside htmlString the var not printed on the page(I cut the all content of htmlString because its very long)

<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <script type="text/javascript">  
      var htmlString="&lt;!doctype html&gt;&lt;html&gt;&lt;head&gt;&lt;metahttp-equiv=&quot;content-type&quot; cotring)";

   document.write(htmlString);
    </script>

  </body>

</html>

Thanks

+2  A: 

In order to pull remote pages with file_get_contents it requires fopen_wrappers to be enabled. If your host has this disabled and they allow cURL() I would go that route. cURL is also generally faster then file_get_contents, so that may be a deciding factor as well.

EDIT:

The problem you are having, particularly with google, is that it uses JS Code in the webpage. I just var_dump'ed the htmlString and it all displayed fine. But when putting it back into the JavaScript it went caput. The error that came back was an Unterminated String Literal (via Firefox's Error Console) on Line 8. Probably due to some single quotes etc. In my testing I tried htmlentities(), which worked and displayed the data to the browser. The section to change is:


$htmlString= htmlentities(file_get_contents('http://google.com'));

And it should work like you want it to.

Brad F Jacobs
Its enable because I get the content of the page, I just not succed to print it on my webpage
Yosef
Added something I think is the solution (at least it worked in my tests). See the edit section above.
Brad F Jacobs