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

标签 php javascript html

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

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

我使用了 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 - 通过 PHP 脚本中运行的操作将大型变量存储在 HTML5 持久存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7598889/

相关文章:

php - 正则表达式限制与之前匹配的字符串的匹配

php代码来匹配数据库中的列日期并显示客户数据

php - 在 PHP while 循环中获取变量值的总和

javascript - Bootstrap 4 输入分机值

javascript - 在 html 选项标签中使用 javascript 变量

php - 从函数调用递归函数只返回第一个值

javascript - 如何在 Javascript 中出现错误时中断

javascript - 用于具有不同端点的嵌套 api 资源的 AngularJs ngResource

html - 使用 bootstrap 创建布局

html - Bootstrap 菜单下拉显示在后台