关联数组值中的转义撇号 [英] An escaped apostrophe in associative array value

查看:60
本文介绍了关联数组值中的转义撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从MySQL表动态构建的关联数组,如下所示:

I have an associative array that is built dynamically from a MySQL table like so:

array(['p.id'] => 3, ['p.fname'] => 'Bill', ['p.lname'] => 'O\'Reilly')

这是一个大型数组,以这种方式构建是出于太长的原因,无法在此处介绍.如您所见,问题是当我们尝试访问['p.lname']的值时,我们得到"O \"

This is a large array and was built this way for reasons that are too long to go into here. The problem, as you can see is that when we attempt to access the value of ['p.lname'] we get "O\"

任何人都对如何解决这个问题有任何想法,而无需修改数组的构建方式?我目前正在解决斜杠和内部撇号的问题,但是我宁愿将撇号保留在适当的位置而只是去掉斜杠.

Anyone have any ideas on how to get around this without modifying the way the array is built? I am currently stripping the slashes and internal apostrophes as a work around, but would prefer to leave the apostrophes in place and just strip the slashes.

由于输出变成如下形式的输入,这使情况变得复杂:

This is complicated by the fact that the output goes into a form input like so:

$field = "<input type='text' name='$input_unique_id' style='width:$width;' value='$array_value' />";

推荐答案

带斜线

到目前为止,这是正确的.

That's right so far.

这很复杂,因为输出会变成输入形式

This is complicated by the fact that the output goes into a form input

这是另一个问题:HTML元素属性使用单引号,因此不能在那样的属性值中使用它们*.属性值始终应使用 htmlspecialchars 进行转义(您必须设置ENT_QUOTES 标志(在这种情况下)

And this is a different issue: You use single quotes for the HTML element attributes, so you cannot use them in the attribute value like that*. Attribute values always should be escaped with htmlspecialchars (you will have to set the ENT_QUOTES flag in this case)

*)您当前的HTML(应用了带斜线)如下所示:

*) your current HTML (with stripslashes applied) looks like this:

<input value='O'Reilly'>

Reilly'部分无效,因此被忽略,这留下了 value ='O'

The Reilly' part is invalid and thus ignored, this leaves value='O'

这篇关于关联数组值中的转义撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆