簡體   English   中英

PHP在JavaScript中轉義

[英]PHP escape in javascript

我正在嘗試從數據庫查詢中輸出項目描述。

這是一段代碼,應輸出帶有名稱等的描述...

<?PHP

$type = "item";
$limit = 16;

$preparedStatement = $SQL->prepare('SELECT * FROM z_shop_offer WHERE offer_type = :type LIMIT :limit');

$preparedStatement->bindParam(':type', $type, PDO::PARAM_STR);
$preparedStatement->bindParam(':limit', $limit, PDO::PARAM_INT);
$preparedStatement->execute();

if ($preparedStatement->rowCount() > 0) {
        // Define how we want to fetch the results
        $preparedStatement->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($preparedStatement);

        foreach ($iterator as $item) {

        echo '
        <div class="ServiceID_Icon_Container" id="ServiceID_Icon_Container_'.$item['id'].'">

          <div class="ServiceID_Icon_Container_Background" id="" style="background-image:url('.$layout_name.'/images/serviceid_icon_normal.png);">

            <div class="ServiceID_Icon" id="ServiceID_Icon_'.$item['id'].'" style="background-image:url(' . $config['site']['item_images_url'] . $item['itemid1'] . $config['site']['item_images_extension'] . ');" onclick="ChangeService('.$item['id'].', 12);" onmouseover="MouseOverServiceID('.$item['id'].', 12);" onmouseout="MouseOutServiceID('.$item['id'].', 12);">

              <div class="PermanentDeactivated">
                <span class="HelperDivIndicator" onmouseover="ActivateHelperDiv($(this), \''.$item['offer_name'].'\', \''.htmlentities($item['offer_description']).'<BR><BR>\', \'\');" onmouseout="$(\'#HelperDivContainer\').hide();">
                  <div class="ServiceID_HelperDiv"></div>
                </span>
              </div>

            </div>
          </div>
        </div>
        ';

        }
}
?>

我已經嘗試過htmlentitieshtmlspecialcharsaddslashes

這是存儲在數據庫中的描述(在此數據庫上,它不再顯示帶有描述的工具提示。

激活一小時1.5倍的經驗。 它只有一個電荷。 在活動時使用任何其他注射只會增加時間,而不是經驗。 如果您注銷或死亡,時間不會停止。

如何正確轉義/輸出描述?

您可能遇到的問題是,您需要將轉義數據加倍。 你需要 :

  • 首先將其轉義為HTML
  • 第二個JavaScript。

嘗試在您的描述字段上運行此PHP函數:

function fix_js($string){

     return strtr(
              $string, 
              array(
                '\\' => '\\\\', 
                "'" => "\\'", 
                '"' => '\\"', 
                "\r" => '\\r', 
                "\n" => '\\n', 
                '</' => '<\/')
            );
}

並告訴我們是否可以解決您的問題

暫無
暫無

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

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