Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数据库中转义使用alt属性的字符串_Php_String_Escaping - Fatal编程技术网

Php 从数据库中转义使用alt属性的字符串

Php 从数据库中转义使用alt属性的字符串,php,string,escaping,Php,String,Escaping,这应该相当容易,但我不明白我在这里遗漏了什么: 我有我的数据库,我想从中填充alt属性 我的代码如下: <img src="admin/uploads/retouch/'.$list->thumbnail.'" class="thumb-image" alt="'.utf8_encode(addslashes($list->titulo)).'" /> 缩略图。“”class=“thumb image”alt=“”.utf8_编码(addslashes($list-

这应该相当容易,但我不明白我在这里遗漏了什么:

我有我的数据库,我想从中填充alt属性

我的代码如下:

  <img src="admin/uploads/retouch/'.$list->thumbnail.'" class="thumb-image" alt="'.utf8_encode(addslashes($list->titulo)).'" />
缩略图。“”class=“thumb image”alt=“”.utf8_编码(addslashes($list->titulo))。”/>
在这种情况下,我的数据库中“titulo”中的内容“在它上面,我在代码中得到了这个结果:

  <img ti!\""="" por="" ¡hazlo="" manos.="" en="" estÁ="" prestaciones="" tus="" mejorar="" alt="\" class="thumb-image" src="admin/uploads/retouch/noticia_default.png">


数据库中的句子是:“MEJORAR TUS PRESTACIONES ESTÁEN TUS MANOS.”HAZLO POR TI!

使用htmlspecialchars或htmlspecialchars来转义标记属性中的字符串。

您需要使用PHP函数htmlspecialchars()来转义此类内容。

您应该使用
htmlspecialchars
而不是
添加斜杠
,使用utf8选项

alt="'.htmlspecialchars($list->titulo, ENT_QUOTES, 'UTF-8').'"

您应该对htmlentities进行编码,而不是添加斜杠:

alt="'.htmlentities($list->titulo, ENT_QUOTES, 'UTF-8').'"

你在页面上设置utf-8了吗
如果未正确编码,也可以尝试在
$list->titulo
周围添加
utf8\u编码。是的,utf8\u编码成功了,非常感谢,我今天学到了一些新东西:可能的