2011-09-29 60 views
0

我将介绍开始为这实际上应该做的事情,从一个动作运行存储在HTML5永久存储大量变量...在PHP脚本

抓取网页的全部内容,转成字符串,并保存到持久性存储中。然而,由于某种原因,它只是......不会?

我用php的html实体,然后JSON Stringify,但它只是无法正常工作。

我的代码如下...

//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>"; 

编辑:

该源代码将呈现以下

<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> 
+0

您遇到了什么问题?什么错误信息?另外,请考虑使用PHP的'json_encode()'在PHP端执行字符串化。 –

+0

我没有想过/尝试过。在查看页面源代码时,它只是呈现整个文本,显然是用html实体格式化的,但不会将任何关键词添加到商店中... –

回答

2

你的意思是:

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

目前,它正在评估于:

tokens[something = test]; 

这是无效的,你想要什么没有做:

  • 一切属性名内部发生;什么都没有设置
  • 您没有报价这将可能陷入困境的事情

如果你的代码返回此:

<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> 

那么它是无效的:

  • 这是<script type='text/javascript'>
  • 我不知道你的意思是0 = tokenvalue here(你存储的东西号码为0,这是不可能的)。你不是说tokens[0] = tokenvalue
  • 有换行,所以你应该删除它们,你现在有一个未终止字符串
+0

问题实际上并不在于令牌部分。这是渲染罚款。它在toStore部分... –

+0

@Graeme Leighfield:什么是'tokens'和'toStore'?如果它们是函数,则应该使用'()'而不是'[]'。 – pimvdb

+0

它们是数组,$ t是引用该数组中键的计数器。 –