php - 字符串处理 Java/html/php

标签 php javascript html

echo '<button class="modalInput" title="Is there an error in this question, report it here." rel="#prompt" onClick="params('.$item->catid.','.$item->id.',"'.$item->quote.'","'.$item->author.'");">';

现在 - 这工作得很好 - 如果 $item->quote 和 $item->author 不包含任何值,例如:

我叔叔家 谁说“Ich bin ein Berliner”?

那么参数字符串就会变得非常困惑......

  <button class="modalInput" title="Is there an error in this question, report it here." rel="#prompt" onClick="params(21,12,"Who said "ich bin ein Berliner"","George Bush");">

我该如何解决这个问题?

<小时/>

还是不行

   echo '<button class="modalInput" title="Is there an error in this question, report it here." rel="#prompt" onClick="params('.$item->catid.','.$item->id.',\''.htmlspecialchars($item->quote).'\',\''.(string) htmlspecialchars($item->author).'\');">';

变成

  <button class="modalInput" title="Is there an error in this question, report it here." rel="#prompt" onClick="params(29,29,'In a FOX TV show, what did 'The OC' stand for','Orange County');">

最佳答案

使用htmlspecialchars函数,它将编码(转义)特殊字符,例如" 将变为 ":

echo htmlspecialchars($item->quote);

关于php - 字符串处理 Java/html/php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252473/

相关文章:

php - 简单的 php/mysql 登录表单安全性

javascript - NodeJS 自定义模块未定义

javascript - 获取 Iframe 水平滚动条位置 javascript

javascript - jQuery .active HTML 展示

javascript - Jquery 效果在鼠标悬停时显示 <li><a> 菜单元素的溢出文本

php - 如何自动更新MYSQL数据库中的字段数据

php mysql - 在单个查询中获取多于1条记录

javascript - 表格中只有第一行按钮起作用

javascript - 在 JavaScript 中添加 json 对象作为子对象

javascript - 如何在按键时将字符附加到 JavaScript 中文本框/文本区域中的选定文本