PHP字符串格式问题

时间:2013-10-10 07:43:37

标签: php jquery html

我正在使用工具提示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 =“

7 个答案:

答案 0 :(得分:1)

你说你需要生成这个:

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

...在title属性中:

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

答案 1 :(得分:0)

使用转义字符\'

$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>';

答案 2 :(得分:0)

使用\

   $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>';

答案 3 :(得分:0)

您缺少在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混合在您的代码中。你需要分开它。

答案 4 :(得分:0)

尝试将其保存在变量转义引号中,而不是在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'].'">';

答案 5 :(得分:0)

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

$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?";

答案 6 :(得分:0)

试试这个:

$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>';