开发者

protect my html div [duplicate]

开发者 https://www.devze.com 2023-04-05 03:28 出处:网络
This question already has an answer here: Closed 11 years ago. Possible Duplicate: show html code inside box like yahoo html messages
This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

show html code inside box like yahoo html messages

first, i am sorry for moderators .. i have made this question yeasterday and got n开发者_如何学Co answers!

i have html form

allow users to post them html code and then i show it by my php

here is my php code

<?php

 $html = '<div id="my_box" style="border:1px #000 solid">'; //my own div!
 $_POST['user_html'] = '</div><p>Any thing</p>'; //html code of visitor! , i want display it inside my own div above
 $html .= $_POST['user_html'];
 $html .= '</div>'; //close my own div!
 echo $html;

?>

if you tried this code you will find the user html code displaying outside my own div box

because his code is start with </div> <-- he closed my div!

So , how i can fix it and display his code without affect my own div ?

i need something like iframe!

--NOTE 1

the story above is just to make you understand .. i am not crazy to make project do this stupid job!

--NOTE 2

the php codes is just for example to make you understand .. my project is based html i want fix it by java or html .. not php


Another example

Yahoo , Gmail html messages

they display the html messages inside them html box

i have tired searching ,,,


You need to escape the content put in by the user. Thus it gets displayed as </div><p>Any thing</p> and isn't interpreted.


You probably want to use the htmlentities() function:

$html = '<div id="my_box" style="border:1px #000 solid">'; //my own div!
$_POST['user_html'] = '</div><p>Any thing</p>'; //html code of visitor! , i want display it inside my own div above
$html .= htmlentities($_POST['user_html']);
$html .= '</div>'; //close my own div!
echo $html;

Edit: Actually, that'll only work if you want to display the raw HTML. If you want it to be rendered as HTML, then you'll have to use an iframe somehow. Sorry, I don't know how you'd do this (well, I do, but it's long and convoluted and someone else can probably help better).

0

精彩评论

暂无评论...
验证码 换一张
取 消