php php代码中的img标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15218737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:48:41  来源:igfitidea点击:

img tag in php code

phpimage

提问by user1973003

How can I use img tag in php code?:

如何在 php 代码中使用 img 标签?:

This is the code I have so far:

这是我到目前为止的代码:

$message = $message."...<br /><a href=\"".$mybb->settings['bburl']."/".$announcement['threadlink']."\">".$lang->messagemore."</a>";

And I wish to use an image instead of ".$lang->messagemore."but I don't know how to use img tag in php :| I have tried googling but I only find some tag info from html but not php code.

我希望使用图像而不是 ".$lang->messagemore."但我不知道如何在 php 中使用 img 标签:| 我试过谷歌搜索,但我只能从 html 中找到一些标签信息,而不是 php 代码。

<img src="/res/gif/bullet_info_sq.gif" alt="" />

But I wish to use that in php so I could use your help.

但我希望在 php 中使用它,以便我可以使用您的帮助。

Thank you very much!

非常感谢!

回答by Jon Newmuis

tl;dr

tl;博士

Replace: $lang->messagemore

代替: $lang->messagemore

With: <img src="/res/gif/bullet_info_sq.gif" alt="" />

和: <img src="/res/gif/bullet_info_sq.gif" alt="" />



When you replace your PHP variable with the imgtag, the HTML must still remain within the quotes of the PHP string, and thus the actual quotes for your imgtag must be escaped, just as you've done for your atag. You may also opt to use single quotes, to avoid having to escape. This should result any one of the following equivalent code snippets (I've added whitespace for readability):

当您用img标记替换 PHP 变量时,HTML 仍必须保留在 PHP 字符串的引号内,因此img必须对标记的实际引号进行转义,就像对a标记所做的那样。您也可以选择使用单引号,以避免转义。这应该会产生以下任一等效代码片段(为了便于阅读,我添加了空格):

Double quotes (with escaping):

双引号(带转义):

$message = $message . "...<br />
<a href=\"" . $mybb->settings['bburl'] . "/" . $announcement['threadlink'] . "\">
    <img src=\"/res/gif/bullet_info_sq.gif\" alt=\"\" />
</a>";

Single quotes for HTML attributes:

HTML 属性的单引号:

$message = $message . "...<br />
<a href='" . $mybb->settings['bburl'] . "/" . $announcement['threadlink'] . "'>
    <img src='/res/gif/bullet_info_sq.gif' alt='' />
</a>";

Single quotes for PHP string:

PHP 字符串的单引号:

$message = $message . '...<br />
<a href="' . $mybb->settings['bburl'] . '/' . $announcement['threadlink'] . '">
    <img src="/res/gif/bullet_info_sq.gif" alt="" />
</a>';

回答by Raptor

Just echo the HTML code ( in your case , the <img>tag ) in PHP , just like you did for <a>tag.

只需<img>在 PHP 中回显 HTML 代码(在您的情况下为标记),就像您对<a>标记所做的一样。

回答by Edwin Alex

You can use like this,

你可以这样使用,

$message = $message."...<br /><a href=\"".$mybb->settings['bburl']."/".$announcement['threadlink']."\"><img src='/res/gif/bullet_info_sq.gif' alt='' /></a>";

回答by Sankalp Mishra

use this:

用这个:

$message = $message."...<br /><a href=\"".$mybb->settings['bburl']."/".$announcement['threadlink']."\">".'<img src="/res/gif/bullet_info_sq.gif" alt="" />'."</a>";

回答by Sumit Bijvani

Use this

用这个

$message = $message."...<br /><a href=\"".$mybb->settings['bburl']."/".$announcement['threadlink']."\"><img src=\"/res/gif/bullet_info_sq.gif\" alt=\"\" /></a>";

回答by Alfonso Tienda

It works like that:

它是这样工作的:

$message = $message."...<br /><a href=\"".$mybb->settings['bburl']."/".$announcement['threadlink']."\">"."<img src='".$aImageURL."' alt="" /></a>";

Simply replace de img src with a PHP variable. ($aImageURL will be the URI of the image)

只需将 de img src 替换为 PHP 变量即可。($aImageURL 将是图像的 URI)

Take a look at the quotes (I put simply quotes in the src attribute, you can do that)

看看引号(我在 src 属性中简单地加上引号,你可以这样做)

回答by Waqar Alamgir

So simple, do not create mess with double quotes " PHP optimally used single quotes '. Use standard html within string!

如此简单,不要乱用双引号“PHP 最佳使用单引号 '。在字符串中使用标准 html!

echo ' <img src="php.gif" /> ';