简体   繁体   English

如何设置除图像HTML代码之外的所有HTML代码显示

[英]How can I set all HTML code display except image HTML code

Please see the picture below. 请看下面的图片。 I need to set my page like below. 我需要像下面那样设置我的页面。

I'm using PHP, to show the HTML code I got success. 我正在使用PHP来显示我获得成功的HTML代码。

<?php
$post = "<p>I like this</p>";
$get_post = htmlspecialchars($post);
?>

and the result will be : <p>I like this</p> <-- All html code display. 结果将是: <p>I like this</p> < - 所有html代码显示。

But now, if I have an image file, 但现在,如果我有一个图像文件,

<img src="logo.png"/>

It won't display the image, but html display. 它不会显示图像,但会显示html。

What I want is, how can I set all HTML code display except image HTML code <img> ? 我想要的是,如何设置除图像HTML代码<img>之外的所有HTML代码显示?

Please see the image below for example. 请参阅下面的图片。

在此输入图像描述

Additional Information 附加信息

1. We only have 1 variable, that's $post
2. Assume $post retrieve from database

Example 1: $post = "<html><p>a</p><img src='logo.png'/></html>";
Example 2: $post = "<img src='logo.png'/>";

Please see the picture above for example. 请参见上面的图片。

I think concat will do the thing. 我认为concat会做这件事。 Comments are welcomed. 欢迎评论。

<?php
$post1 = "<p>I like this</p>";
$image= "<img src="logo.png"/>";
$post2= "<p>I like this Too</p>";
$get_post = htmlspecialchars($post1)." ".$image." ".htmlspecialchars($post2);
?>

Update 更新

use htmlspecialchars_decode() 使用htmlspecialchars_decode()

for img src you can visit here or this can solve your question. 对于img src你可以访问这里,或者可以解决你的问题。

you can check if the string have one img tag, then use if condition to get the output: 你可以检查字符串是否有一个img标签,然后使用if条件来获取输出:

$post = "<img src='logo.png'/>";
//$post = '<p>I like this</p>';

if(strpos($post, "<img") != false)
{
     echo $post;
}else{
    $get_post = htmlspecialchars($post);
    echo $get_post;
}

Still confused of your question for my bad English. 仍然困惑你的问题我的英语不好。 Perhaps this helps. 也许这有帮助。

<?php
$post='<img src="image/america.png"/>';
echo $post;
 ?>

UPDATED : 更新

<?php
$post='<img src="image/america.png"/>';
//$post='<p>Hallo</p>';
$detect=substr($post,0,4);
if ($detect){
echo $post;
}
else{
$get_post = htmlspecialchars($post);
echo $get_post;
}
?>

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

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