如何确保包含引号的属性值不会中断格式? [英] How do I ensure attribute values containing quotes do not break formatting?

查看:83
本文介绍了如何确保包含引号的属性值不会中断格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我正在存储帖子的标题,当我在页面上创建链接时,我将该帖子的标题作为标题属性。

In my database I am storing the title of posts and when I create the links on the page I put the title of the post as the title attribute.

<a title='here is the description'>bluah</a>

但是,页面上发生的是在我的描述中有一些打破它的字符。打破

But what is happening on the page is that in my descriptions there are characters that are breaking it. Example of a post title that breaks

<a title='I don't hanging out with friends, had a great time seeing Sarah again ;p '>bluah</a>

我想我需要做一些HTML编码,但我不知道什么时候/我应该在哪里

I think I need to do some HTML encoding, but I am not sure when/where I should be doing that.

谢谢

推荐答案

使用 htmlspecialchars 输出你的字符串

htmlspecialchars("I don't",ENT_QUOTES); // returns I don&#039;t

这也将照顾其他HTML字符,如< &安培; >如手册中所述,

That will also take care of other HTML characters such as < & > as explained in the manual,

http://php.net/manual/en/function.htmlentities.php

这篇关于如何确保包含引号的属性值不会中断格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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