将PHP脚本中运行的操作中的大型变量存储在HTML5持久存储中

Storing a large variable in HTML5 persistent storage from an action run in a PHP script

本文关键字:存储 HTML5 变量 操作 PHP 脚本 运行 大型      更新时间:2023-09-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>

你的意思是:

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吗?
  • 有换行符,所以你应该删除它们,因为你目前有一个未终止的字符串

快速搜索后找到解决方案

无终止字符串字面值

的常见来源

是php代码的换行害死了它。

这很好地修复了它

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