簡體   English   中英

PHP 中的轉義字符串供 JavaScript 使用

[英]Escape String in PHP For JavaScript To Use

我正在通過 AJAX 接收一些代碼並像這樣處理它:

$verifiedSubject = addslashes(htmlentities($_REQUEST['subject']));
$verifiedBody = addslashes(htmlentities($_REQUEST['body']));
$verifiedAttachment1 = addslashes(htmlentities($_REQUEST['attachment1']));
$verifiedAttachment2 = addslashes(htmlentities($_REQUEST['attachment2']));

echo '<div id="subject" style="text-decoration: underline; cursor:pointer; display: inline; margin-bottom: 2%;" onclick=\'readmore("'.json_encode($verifiedSubject).'", "'.json_encode($verifiedBody).'", "'.json_encode($verifiedAttachment1).'", "'.json_encode($verifiedAttachment2).'")\'>';
echo $_REQUEST['subject'];
echo '</div>';

在上面的代碼中,我試圖將任何 HTML 代碼轉換為實體,添加斜杠以轉義單引號和雙引號,然后json_encode()它讓 JavaScript 在onclick函數中處理。

但是,當單擊文本以啟動onclick我收到此錯誤:

參數列表后未捕獲的 SyntaxError: missing )

我嘗試了各種 PHP 函數來嘗試正確轉義此字符串,但似乎沒有任何效果。 有人可以幫我嗎?

頁面源更新:

<script>
var date = "Monday, November 16th, 2015 Announcements";
formatAnnouncement( '55', 
                    'Hi',
                    '<b  rgb(0, 0, 0); font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;\">Lorem Ipsum</b><span  rgb(0, 0, 0); font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;\">&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>',
                    '0',
                    '',
                    '',
                    '******',
                    '2015-11-16 16:53:23',
                    date
                    );
</script>

你這樣做是錯的。 json_encode() 可以完成您需要的一切操作,只需將結果字符串用作可分配的“值”。 例如

<?php

$foo = 'bar';
?>

<script>
    var test1 = <?php echo json_encode($foo); ?>;   // this works
    var test2 = "<?php echo json_encode($foo); ?>; // this doesn't

你最終會得到的是:

var test1 = "bar";
var test2 = ""bar"";  //syntax error - two empty strings with undefined var between them

所以你的代碼中應該有:

echo '<div id="subject" style="text-decoration: underline;
   cursor:pointer; display: inline; margin-bottom: 2%;"
  onclick=\'readmore('.json_encode($verifiedSubject). ', '. 
                      ^--no "                          ^-^--ditto 
  etc...

不需要添加斜杠,因為 json 將轉義將您的值轉換為“安全”javascript 字符串表示所需的一切。 但是,由於您將此 json 文本嵌入到 html onclick 中,因此很可能需要 htmlentities,以防止"字符”從 html 中“脫離”。

看起來它回顯的結果引號太多了。 嘗試這個:

echo '<div id="subject" style="text-decoration: underline; cursor:pointer; display: inline; margin-bottom: 2%;" onclick=\'readmore('.json_encode($verifiedSubject).', '.json_encode($verifiedBody).', '.json_encode($verifiedAttachment1).', '.json_encode($verifiedAttachment2).')\'>';
echo $_REQUEST['subject'];
echo '</div>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM