如何在两个标签之间添加斜杠


How to addslashes between two tags?

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

text is->alt="打开包装后,Isaac Sutton收藏的其他画作的照片"

应该是->alt="打开包装后Isaac Sutton收藏的其他画作的照片"

<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' />

"如何添加斜杠?"

使用addslashes()

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

话虽如此,由于'不是HTML转义符,因此即使使用斜杠,这在alttitle属性中也不起作用。

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

$txt = htmlentities($txt, ENT_QUOTES);

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

<img alt="Photo od additional paintings from Isaac Sutton's collection after unpacking" ... />

在HTML属性中使用该值之前,必须对其进行转义。单引号前的反斜杠不起作用。在HTML/XML中,单引号作为一个实体进行转义。

如果您使用DOM来创建HTML,它将负责转义。如果您将HTML创建为文本,则htmlspecialchar()可以完成此任务。

$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"
<?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?>' />