繁体   English   中英

PHP字符串格式问题

[英]PHP string formatting issue

我正在使用工具提示jquery插件,它需要在链接属性中使用单引号,如下所示:

   <a href="#" class="tooltip" title="<img src='my-image.png' width='100' height='100' />

但是我正在尝试将其保存在php变量中,如下所示:

$calendar.= '<div class="event-day tooltip"><a href="event.php?id='.$event['event_id'].'"title="<img src="my-image.jpg" width="500" height="400"/><span class="hb">'.$hour.'</span> '.$event['name'].'</a></div>';

正如您在title属性中看到的那样,由于title =“

您说您需要生成此:

<a href="#" class="tooltip" title="<img src='my-image.png' width='100' height='100' />

...但这是完全无效的HTML。

我怀疑您实际上是想插入一些任意HTML,例如:

 <img src='my-image.png' width='100' height='100' />

...在标题属性内:

 <a href="#" class="tooltip" title="">Foo</a>

这不是特别困难:

<?php
$title = "<img src='my-image.png' width='100' height='100' />";
echo '<a href="#" class="tooltip" title="' . htmlspecialchars($title) . '">Foo</a>';
<a href="#" class="tooltip" title="&lt;img src='my-image.png' width='100' height='100' /&gt;">Foo</a>

使用转义符\\'

$calendar.= '<div class="event-day tooltip"><a href="event.php?id='.$event['event_id'].'"title="<img src=\'my-image.jpg\' width=\'500\' height=\'400\'/><span class="hb">'.$hour.'</span> '.$event['name'].'</a></div>';

采用 \\

   $calendar.= '<div class="event-day tooltip"><a href="event.php?id='.$event['event_id'].'"title="<img src=\'my-image.jpg\' width=\'500\' height=\'400\'" /><span class="hb">'.$hour.'</span> '.$event['name'].'</a></div>';

您缺少在title="之前添加空格的信息

<a href="event.php?id='.$event['event_id'].'" title="<img src="my-image.jpg" width="500" height="400"/><span class="hb">'.$hour.'</span> '.$event['name'].'</a>';
                                           ^^^^^^               

问题是代码中混合了2个属性hreftitle 您需要将其分开。

尝试将其保存在变量转义引号中,而不是在title属性中使用双引号。

另外,由于您要关闭从未打开过的<a><div> ,因此标记是不正确的。 (在下面的示例中进行了更改)

$calendar.= '<div class="event-day tooltip"><a href="event.php?id='.$event['event_id'].'"title="<img src=\'my-image.jpg\' width=\'500\' height=\'400\'/><span class=\'hb\'>'.$hour.'</span> '.$event['name'].'">';

将图像标签存储在变量中,并按如下方式使用它:

$myImg="<img src='my-image.jpg' width='500' height='400'/>";
$calendar.= "<div class='event-day tooltip'><a href='event.php?id=$event[event_id]' title='$myImg'><span class='hb'>$hour</span>$event[name]</a></div>";

另外,在与字符串一起使用时,不需要连接PHP变量。

echo "Hi $myname, how u doing?";

胜过

echo "Hi ".$myname.", how u doing?";

尝试这个:

$calendar = '<div class="event-day tooltip"><a href="event.php?id='.$event['event_id'].' "title="<img src=\'my-image.jpg\' width="500" height="400"/><span class="hb">'.$hour.'</span> '.$event['name'].'</a></div>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM