单引号和双引号一起作为HTML属性值? [英] Single and double quotes together as HTML attribute value?

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

问题描述

我们有这样的代码:

  echo'< input type =textname =myInputvalue =DoubleQuotes/>'; 

绝对不行,因为报价后 Double 结束价值
我们可以使用单引号而不是双引号来修复它。

  echo'< input type =textname =myInputvalue = \'DoubleQuotes \'/>'; 

现在我想使用单引号和双引号作为值。它应该输出她说:我不知道。



有没有办法解决这个问题<使用HTML实体(如& quot; ), htmlentities()或类似函数?

解决方案


有没有办法使用HTML修复它 WITHOUT 实体(如& quot; ), htmlentities()或类似函数?

不,没有。双引号()在HTML属性里有特殊的含义,如果你想把它放到一个属性值中,你必须(这不是真的必须的,而是一个好的如果你在你的问题中使用用双引号分隔的属性,这是必须的)把它写成它的实体& quot; 。没有办法


$ b 实际上甚至< tag attr ='this'> 也是错误的HTML大多数浏览器都可以处理这个问题。然而,它不能帮助你,因为你正在寻找两个引号 - 单引号和双引号 - 而HTML中的其中一个 always 是属性值的分隔符 - 如果你需要属性值中的空格(和你一样)。



但是,别担心。它的工作原理,你可以表达你喜欢的一切,包括你有的报价组合。



实际上PHP是你的负担,转义所有这些角色只需使用 htmlspecialchars 方法为您完成所有工作。在一个PHP字符串中,你有原始文本 - 你认为合适的单引号和双引号 - 逐字。

  $ myString ='她说:我不知道。 
printf('< input type =textname =myInputvalue =%s/>'
,htmlspecialchars($ myString));

只是一个应该演示如何工作的简短示例。 在线演示


We have a code like this:

echo '<input type="text" name="myInput" value="Double " Quotes" />';

Absolutely it doesn't work because the quote after Double ends the value. We can fix it by using single quotes instead of double ones.

echo '<input type="text" name="myInput" value=\'Double " Quotes\' />';

Now I wanna use both single and double quotes as the value. It should outputs She said:"I don't know."

Is there a way to fix it WITHOUT using HTML entities (Like &quot;), htmlentities() or similar functions?

解决方案

Is there a way to fix it WITHOUT using HTML entities (Like &quot;), htmlentities() or similar functions?

No, there is not. The double quote (") has special meaning inside a HTML attribute. If you want to put it into an attribute value, you must (this is not a true must but a good rule of thumb. It's a must if you use attributes delimited by double-quotes as you do in your question) write it as its entity &quot;. There is no way around it.

Actually even <tag attr='this"'> is not wrong HTML, too and most browsers can deal with that. However it doesn't help you because you're looking for both quotes - single and double - and one of these always in HTML is a delimiter of the attribute value - if you need spaces inside the attribute value (as you do).

However, do not worry about that. It works, and you can express everything you like with that, including the combination of quotes you have.

And actually PHP is there for you to take the burden of "escaping" all those characters just with the htmlspecialchars method doing all the work for you. Inside a PHP string you have the original text - with single and double quotes as you see fit - verbatim.

$myString = 'She said: "I don\'t know."';
printf('<input type="text" name="myInput" value="%s" />'
       , htmlspecialchars($myString));

Just a shortened example that should demonstrate how this works. Online demo.

这篇关于单引号和双引号一起作为HTML属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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