【问题标题】:Storing a large variable in HTML5 persistent storage from an action run in a PHP script从 PHP 脚本中运行的操作将大变量存储在 HTML5 持久存储中
【发布时间】:2011-09-30 04:59:26
【问题描述】:

我将首先简要介绍这实际上应该做什么......

获取网页的全部内容,转换为字符串,并保存到持久存储中。但是由于某种原因,它只是...不会?

我使用了 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>

【问题讨论】:

  • 你有什么问题?什么错误信息?另外,考虑在 PHP 端使用 PHP 的 json_encode() 进行字符串化。
  • 我没想到/尝试过。查看页面源代码时,它只是呈现整个文本,显然是 html 实体格式,但不会在商店中添加任何内容...

标签: php javascript html


【解决方案1】:

你的意思是:

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>

那么它是无效的:

  • &lt;script type='text/javascript'&gt;
  • 我不知道0 = tokenvalue here 是什么意思(你在号码0 中存储了一些东西,这是不可能的)。你不是说tokens[0] = tokenvalue吗?
  • 有换行符,因此您应该删除它们,因为您目前有一个未终止的字符串

【讨论】:

  • 问题实际上并不在于令牌部分。这很好。它在 toStore 部分...
  • @Graeme Leighfield:tokenstoStore 是什么?如果它们是函数,则应使用() 而不是[]
  • 它们是数组,$t 是引用数组中该键的计数器。
  • @Graeme Leighfield:您能发布浏览器中返回的代码吗?做类似arr[a = b]; 的事情是相当没有意义的,但也许我误解了。 JavaScript 端实际执行了哪些代码,即解析 PHP 代码时?
【解决方案2】:

快速搜索后找到解决方案

Common sources of unterminated string literal

是 php 代码中的换行符杀死了它。

这很好地解决了它

$str = str_replace(array("\r", "\n"), '', $str);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2011-10-04
    • 2015-09-27
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    相关资源
    最近更新 更多