javascript中的PHP转义


PHP escape in javascript

我正在尝试从数据库查询输出项目描述。

这段代码应该输出带有name等的描述…

<?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>
        ';
        }
}
?>

我已经试过htmlentities, htmlspecialcharsaddslashes了。

这是存储在数据库中的描述(在此,它停止显示带有描述的工具提示。

激活1小时1.5倍经验值。它只有一个电荷。当一个注入处于活动状态时,使用任何其他注入只会叠加它的时间,而不是经历。时间不会停止,如果你注销或死。

如何正确转义/输出描述?

您可能遇到的问题是需要对数据进行双转义。您需要:

  • 首先转义为HTML
  • javascript的
  • 秒。

尝试在你的描述字段上运行这个PHP函数:

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

让我们知道它是否解决了你的问题