【问题标题】:How to correctly escape array in php? [duplicate]如何在php中正确转义数组? [复制]
【发布时间】:2019-06-21 15:55:24
【问题描述】:

我想在 php 中转义一个数组

我尝试使用 addlashes 函数进行转义,但它不起作用

 while($row = $res->fetch_assoc()) 
    {
        $row['name']=addslashes($row['agente']); //escape ?
        $rows[] = $row;
    }

我想逃避一个叫“Antonio cinà”的人的名字,但它不起作用 问题是'à'

【问题讨论】:

  • 输出的是什么?
  • 输出:安东尼奥辛?
  • “转义”取决于输出。对于您想要完成的任务、数据的来源等,不要守口如瓶。对于 JSON 编码,字符集非常重要。

标签: php arrays json escaping


【解决方案1】:

您还可以使用 htmlspecialchars() 而不是 htmlentities() 来转义数组字符串。

while($row = $res->fetch_assoc()) {
    $row['name']= htmlspecialchars($row['agente']); //escape ?
    $rows[] = $row;
}

【讨论】:

    【解决方案2】:

    使用 htmlentities() php 函数转义特殊字符。

    while($row = $res->fetch_assoc()) {
            $row['name']= htmlentities($row['agente']); //escape ?
    
        }
    

    PHP Manual for htmlentities

    【讨论】:

    • 如果您要输出到 HTML,当然可以。
    • 我尝试了,但是 htmlentities 清理了变量
    • 我想转义字符串然后转成json所有数组
    猜你喜欢
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 2022-01-14
    • 1970-01-01
    • 2017-08-16
    • 2017-09-09
    相关资源
    最近更新 更多