0

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.

enter image description here

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.

HiDayurie Dave
  • 1,791
  • 2
  • 17
  • 45

3 Answers3

0

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.

Community
  • 1
  • 1
Nabin Kunwar
  • 1,965
  • 14
  • 29
  • Hi, how about if I have just $post1? In $post1, I want the function detect show all HTML code except, so all html code will display except – HiDayurie Dave Feb 18 '15 at 04:21
0

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;
}
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Afsar
  • 3,104
  • 2
  • 25
  • 35
0

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;
}
?>
Joe Kdw
  • 2,245
  • 1
  • 21
  • 38