0

如何在 alt 文本中添加斜线。问题是不需要的单引号。

text is -> alt='开箱后 Isaac Sutton 收藏的照片和其他画作'

应该是 -> alt='Isaac Sutton\'s collection 中打开包装后的照片 od'

<img width='318' height='238' alt='Photo od additional paintings from Isaac Sutton's     collection after unpacking' src='[!--$wcmUrl('resource','groups/public/documents/image/kppcont_083705.jpg')--]' title='Additional paintings from Isaac Sutton's collection after unpacking' />
4

4 回答 4

4

“怎么加斜线?”

通过使用addslashes()

$txt = "Photo od additional paintings from Isaac Sutton's collection after unpacking";
$txt = addslashes($txt);

话虽如此,即使使用斜线,这也不适用于您的altandtitle属性,因为\它不是 HTML 转义字符。

您要么需要在<img>标签属性中使用双引号,要么htmlentities()像这样使用:

$txt = htmlentities($txt, ENT_QUOTES);
于 2013-11-25T15:57:36.030 回答
2

在 alt 标记周围使用双引号,而不是单引号。

<img alt="Photo od additional paintings from Isaac Sutton's collection after unpacking" ... />
于 2013-11-25T15:57:35.453 回答
1

您必须先转义该值,然后才能在 HTML 属性中使用它。单引号前的反斜杠不起作用。在 HTML/XML 中,单引号作为实体进行转义。

如果您使用 DOM 创建 HTML,它将负责转义。如果您将 HTML 创建为文本,则 htmlspecialchars() 可以完成这项工作。

$text = "Additional paintings from Isaac Sutton's collection after unpacking";

var_dump(htmlspecialchars($text, ENT_QUOTES | ENT_HTML401));

输出:

string(72) "Additional paintings from Isaac Sutton&#039;s collection after unpacking"
于 2013-11-25T15:58:37.787 回答
0
<?php
$alt=htmlentities("Photo od additional paintings from Isaac Sutton's collection after unpacking");
?>
<img src="aamra24_ahmed_shafi1.jpg" width="473" height="347" alt="<?=$alt?>" />
<img width='318' height='238' alt='<?=$alt?>' src='[!--$wcmUrl('resource','groups/public/documents/image/kppcont_083705.jpg')--]' title='<?=$alt?>' />
于 2013-11-25T16:12:56.957 回答