简体   繁体   中英

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
$post = "<p>I like this</p>";
$get_post = htmlspecialchars($post);
?>

and the result will be : <p>I like this</p> <-- All html code display.

But now, if I have an image file,

<img src="logo.png"/>

It won't display the image, but html display.

What I want is, how can I set all HTML code display except image HTML code <img> ?

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. 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()

for img src you can visit here or this can solve your question.

you can check if the string have one img tag, then use if condition to get the output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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