views:

40

answers:

2

Hi. I'm having the following problem: I have a page where a user inserts text inside a textarea and then I output the contents of that textarea in another page (php).

If the user decides to insert, for example input type="text" name="_name" (can't use < and > or the text won't show, which is exactly my problem) then when showing the output I will have a textbox shown. I want to force text only to appear. Thank you.

+2  A: 

If you want to output some special HTML characters, you can use function htmlspecialchars().

$textarea = '<input type="text" />';
echo htmlspecialchars($textarea);

This outputs &lt;input type=&quot;text&quot; /&rt;
It will be rendered properly as <input type="text" /> by the browser.

michal kralik
Thank you. Exactly what I needed.
elvispt
+1  A: 

Use htmlspecialchars() to encode special HTML characters to their HTML entities (especially < to &lt; and & to &amp;).

Example:

<?php
echo "Pi is <4"; // HTML syntax error, '<' needs to be encoded properly
echo htmlspecialchars("Pi is <4"); // Ok, outputs 'Pi is &lt;4', which is proper HTML
Frxstrem