开发者

How to make a textarea ouput textarea code without breaking?

I created a form where users can enter html code and it outputs their code in another textarea. The problem is that if the html the user enters has a textarea in the code, the 开发者_开发技巧in their code breaks my textarea form. I see other sites display any html correctly so how is this done without breaking the form and allowing the user to copy it so that it still remains as and not some converted code so they can paste it on their webpage?

Ah crap yeah I figured it out, in fact the problem wasn't with the htmlspecialchars code alone I forgot to add a return to one of my functions haha. Thanks guys.


Represent characters that have special meaning in HTML using entities. Since you are using PHP, use htmlspecialchars


There are millions and millions of ways to do this. The easiest is to use htmlspecialchars or htmlentities on the user's input. This will make a visual </textarea> in the textarea box without closing it. This actually turns it into &lt;/textarea&gt;. htmlspecialchars transforms less characters than htmlentities and usually makes more sense to use in a situation like this, but do your research.

strip_tags() is also a possibility.

You can also use a regular expression with PCRE, or even str_replace() or other string manipulation functions to strip off the textarea, convert the special characters, etc.

PECL also as a BB code extension you can use if you still want your users to be able to enter some for of tags to style their output.


<textarea><?php echo htmlentities($code); ?></textarea>


You have to transform the html code into symbols, so it is not treated as html.

Use the function htmlentities() on the textarea content before echoing it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜