您好,我正在尝试从多行数据库中获取值。我通常按 echo $row['description'];
回显该值,结果类似于
the new part 3
he is working and prepared to enter the order.
The witness sees me and talks example in preparation
现在,如果我尝试使用 JavaScript 或 jQuery 获取此值,这会导致Unexpected Token Illegal
.如何使这个多行值作为正常的段落或句子在单行中。
尝试str_replace("'n",' ',$row);
这会用空格替换换行符。如果这提供了额外的空间,那就让它str_replace("'n",'',$row);
你应该正确地转义Javascript字符串:
echo str_replace([''n', ''r', ''t', "'", '"', ''''], ['''n', '''r', '''t', "'''", '''"'], $row['description']);
您可能还有其他字符应该逃脱,但我想我已经涵盖了您的字符串中可能存在的常见字符。
下面是示例代码:
<?php
function js_escape($str)
{
$str = str_replace("''", "''''", strval($str));
$str = str_replace("'", "'''", $str);
$str = str_replace("'r", "''r", $str);
$str = str_replace("'n", "''n", $str);
$str = str_replace("'t", "''t", $str);
$str = str_replace("</script>", "</'+'script>", $str);
return $str;
}
?><script type="text/javascript">
var a = '<?php echo js_escape('text with special symbols'); ?>';
</script>