通过PHP设置html属性的最佳做法是什么? [英] What's the best practice to set html attribute via PHP?

查看:135
本文介绍了通过PHP设置html属性的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < span title =< p>在PHP中完成这项工作时,可能遇到以下问题: ?php echo $ variable;?>> ... 

问题是如果 $ variable 包含双引号,应将其更改为 \



这还不是全部内容:

 < span title ='<?php echo $ variable;? >'> ... 

在这种情况下,我们需要将单引号改为 \',但保留双引号。



那么我们如何以一般属性的方式来做到这一点?解析方案

php.net/htmlspecialcharsrel =noreferrer> htmlspecialchars

 < span title =<?php echo htmlspecialchars($ variable); ?>> 

您可能想设置第二个参数(

唯一的潜在风险是<$ c )到 ENT_QUOTES

$ c> $ variable 可能已经被编码,所以你可以设置最后一个参数( $ double_encode )至 false


When doing this job in PHP,one may meet this kind of issue:

<span title="<?php echo $variable;?>">...

The problem is that if $variable contains double quotes,should change it to \"

And that's not the whole story yet:

<span title='<?php echo $variable;?>'>...

In this case,we need to change single quotes to \',but leave double quotes as is.

So how can we do it in a general property manner?

解决方案

You always want to HTML-encode things inside HTML attributes, which you can do with htmlspecialchars:

<span title="<?php echo htmlspecialchars($variable); ?>">

You probably want to set the second parameter ($quote_style) to ENT_QUOTES.

The only potential risk is that $variable may already be encoded, so you may want to set the last parameter ($double_encode) to false.

这篇关于通过PHP设置html属性的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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