转义 PHP 输出的 HTML 属性的双引号 [英] Escape double quotes of HTML attributes output by PHP

查看:55
本文介绍了转义 PHP 输出的 HTML 属性的双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在编写 PHP 时我会让它输出一些像这样的 HTML -

Often when writing PHP I'll have it output some HTML like this -

echo "<a href="../" title="link title">".$link_text."</a>";

显然这不会解析,因为我需要转义 <a> 元素的属性中的双引号.是否有正则表达式可以快速执行此操作而不是我手动添加反斜杠?

Obviously this won't parse as I need to escape the double quotes in the attributes of the <a> element. Is there a regex that would quickly do this rather than me manually adding the backslashes?

另一件事 - 正则表达式不应该在标签外转义双引号(例如,我附加了 $link_text 变量.

One other thing - the regex shouldn't escape double quotes outside of the tag (e.g. where I've appended the $link_text variable.

有什么想法吗?

推荐答案

你应该只使用单引号:

echo '<a href="../" title="link title">' . $link_text . '</a>';

这篇关于转义 PHP 输出的 HTML 属性的双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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