将多行 PHP 变量传递给 Javascript 函数
Passing Multi-line PHP variable to Javascript function
我正在尝试将从 SQL 数据库读取的包含文本值(html 内容)的 PHP 变量传递给 Javascript 函数。我正在尝试所有不同的方法,例如 json_encode(utf8_encode())
和 preg_replace()
,但其中 none 似乎有效。
这是我的代码:
<?php
$db = new PDO('mysql:host=localhost;dbname=ramtin_data;charset=utf8', 'root', '');
foreach($db->query('SELECT * FROM posts') as $row)
{
$qtitle = $row['title'];
$qcontent = $row['content'];
$qpage = $row['page_id'];
$qcontent = json_encode(utf8_encode($qcontent));
echo "<script type='text/javascript'>
displayPost('$qtitle', '$qcontent');
</script>";
}
?>
只是想提一下,当我在数据库中的 content
字段中插入一行简单的行(包括 "
和 /
)时,上面的代码有效。
我也尝试了以下代码并得到了相同的结果:
$qcontent = preg_replace('/\v+|\\[rn]/','<br/>', $qcontent);
数据库中 content
字段中的值是以下字符串:
<img src="images/death_truck_titleDisplay.jpg" width="400" height="140" class="globalImageStyle" /><p class="postBodyContentParagraph">The work for another Construct2 game "<a href="http://ramt.in">Death Truck</a>" has been started.</p><p class="postBodyContentParagraph">I've had the idea since a few weeks ago and I'm amazed that how much it had developed into better shapes and more complex gameplay ideas only through the first weeks that I've started working on the graphics and the game codes/logic.</p><p class="postBodyContentParagraph">I'm actually done with the truck and pedestrian graphics and movement codes (plus the interaction code between pedestrians and the truck) and have included the game in the projects page as a Work In Progress. You can check some of the graphics and the idea of the whole game (plus some gameplay ideas) in the projects page.</p>
如何将上述值传递给displayPost()
Javascript函数?
更新
问题出在值中的单引号上。接受的解决方案建议使用 $qcontent = str_replace("'", "\'", $qcontent);
将所有 '
替换为 \'
,效果很好。
结果被 json_encode()
引用,我发现使用 content.slice(1, content.length - 2)
移除包装 "
的最简单方法(content
是第二个参数它指的是代码 PHP 部分中的 $qcontent
)在 Javascript 函数内。
因为您将输出用单引号括起来'
,所以您需要对它们进行转义。
$qcontent = str_replace("'", "\'", $qcontent);
我正在尝试将从 SQL 数据库读取的包含文本值(html 内容)的 PHP 变量传递给 Javascript 函数。我正在尝试所有不同的方法,例如 json_encode(utf8_encode())
和 preg_replace()
,但其中 none 似乎有效。
这是我的代码:
<?php
$db = new PDO('mysql:host=localhost;dbname=ramtin_data;charset=utf8', 'root', '');
foreach($db->query('SELECT * FROM posts') as $row)
{
$qtitle = $row['title'];
$qcontent = $row['content'];
$qpage = $row['page_id'];
$qcontent = json_encode(utf8_encode($qcontent));
echo "<script type='text/javascript'>
displayPost('$qtitle', '$qcontent');
</script>";
}
?>
只是想提一下,当我在数据库中的 content
字段中插入一行简单的行(包括 "
和 /
)时,上面的代码有效。
我也尝试了以下代码并得到了相同的结果:
$qcontent = preg_replace('/\v+|\\[rn]/','<br/>', $qcontent);
数据库中 content
字段中的值是以下字符串:
<img src="images/death_truck_titleDisplay.jpg" width="400" height="140" class="globalImageStyle" /><p class="postBodyContentParagraph">The work for another Construct2 game "<a href="http://ramt.in">Death Truck</a>" has been started.</p><p class="postBodyContentParagraph">I've had the idea since a few weeks ago and I'm amazed that how much it had developed into better shapes and more complex gameplay ideas only through the first weeks that I've started working on the graphics and the game codes/logic.</p><p class="postBodyContentParagraph">I'm actually done with the truck and pedestrian graphics and movement codes (plus the interaction code between pedestrians and the truck) and have included the game in the projects page as a Work In Progress. You can check some of the graphics and the idea of the whole game (plus some gameplay ideas) in the projects page.</p>
如何将上述值传递给displayPost()
Javascript函数?
更新
问题出在值中的单引号上。接受的解决方案建议使用 $qcontent = str_replace("'", "\'", $qcontent);
将所有 '
替换为 \'
,效果很好。
结果被 json_encode()
引用,我发现使用 content.slice(1, content.length - 2)
移除包装 "
的最简单方法(content
是第二个参数它指的是代码 PHP 部分中的 $qcontent
)在 Javascript 函数内。
因为您将输出用单引号括起来'
,所以您需要对它们进行转义。
$qcontent = str_replace("'", "\'", $qcontent);