0

I will begin by briefing as to what this actually should do...

Fetch the entire contents of a web page, turn into a string, and save into persistant storage. However for some reason, it just... wont?

Ive used php's html entities, and then JSON Stringify, however it just fails to work.

My code is as follows...

//arrays set above

$url =  "http://www.google.co.uk";

$handle = fopen($url, "r");

$contents = stream_get_contents($handle);

$contents = htmlentities($contents);

echo "<script lang='text/javascript'>var dataString = JSON.stringify('".$contents."'); tokens[".$t." = ".$rowtokens[5]."]; toStore[".$t." = dataString]; alert('CONTENT'); </script>";

EDIT :

That source code renders the following

<script lang='text/javascript'>tokens[0 = tokenvalue here]; toStore[0 = "&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD X... 
//All the rest of the html of the page.
"];localStorage.setItem(token[0], toStore[0]);</script>
Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38
  • What problems are you having? What error messages? Also, consider doing the stringifying on the PHP side with PHP's `json_encode()`. – Jonathan M Sep 29 '11 at 14:35
  • I hadnt thought/tried that. When viewing the pages source code, it just renders the whole text, obviously with the html entities formatting, but doesnt add anythign onto the store... – Graeme Leighfield Sep 29 '11 at 14:38

2 Answers2

2

You mean:

tokens['".$t."'] = '".$rowtokens[5]."';

Currently it is evaluating to:

tokens[something = test];

which is invalid and does not do what you want:

  • Everything is happening inside the property name; nothing is being set
  • You don't have quotes which will probably mess up things

If your code returns this:

<script lang='text/javascript'>tokens[0 = tokenvalue here]; toStore[0 = "&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD X... 
//All the rest of the html of the page.
"];localStorage.setItem(token[0], toStore[0]);</script>

then it's not valid:

  • It's <script type='text/javascript'>
  • I don't know what you mean with 0 = tokenvalue here (you're storing something in the number 0, which is not possible). Don't you mean tokens[0] = tokenvalue?
  • There are newlines, so you should remove them as you currently have an unterminated string
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • The problem doesnt actually lie in the tokens part. that is rendering fine. its in the toStore part... – Graeme Leighfield Sep 29 '11 at 14:58
  • @Graeme Leighfield: What are `tokens` and `toStore`? If they are functions, you should use `()` not `[]`. – pimvdb Sep 29 '11 at 15:00
  • They are arrays, the $t is the counter to reference that key in the array. – Graeme Leighfield Sep 29 '11 at 15:07
  • @Graeme Leighfield: Could you post what code is returned in the browser? Doing something like `arr[a = b];` is rather meaningless, but perhaps I misunderstand. What code is actually executed on the JavaScript side, i.e. when the PHP code is parsed? – pimvdb Sep 29 '11 at 15:09
2

Found the solution after a quick search

Common sources of unterminated string literal

It was the line breaks from php code that was killing it.

This fixed it nicely

$str = str_replace(array("\r", "\n"), '', $str);
Community
  • 1
  • 1
Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38