Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

striping html tags

Status
Not open for further replies.

aarrgghh

Technical User
Sep 5, 2002
60
US
Hello all,

I have set up a forum. I need an easy way to strip tags that is reliable and safe.

Thanks in advance!
 
Or run it through htmlspecialchars(). That way if anyone types any code it'll be converted so that any < or > becomes &lt; and &gt;
 
using htmlspecialcharacters what would the output be for:

<img src=&quot;javascript:alert('hello')&quot;>

and

</ta</table>ble>

?

Thanks again!
 
Yeah I was looking at that one, but not sure that it catches

</ta</table>ble>

unless I wrote a loop function. Don't tell anyone, but I am just trying to be lazy.
 
htmlspecialchars would change
Code:
</ta</table>ble>
into
&lt;/ta&lt/table&gt;ble&gt;

I though this might be best for a forum so that anyone who wants to post any special html characters (&<>&quot;') for legitimate reason, can do so.
The function that DRJ478 is also a good idea. I didn't even know that existed. You learn something new everyday :D
 
You want to strip the tags to keep users from posting HTML or malicious code.
Code:
</ta</table>ble>
will be stripped by strip_tags into
Code:
ble>
.
This means the tag is disabled even though there is a remnant of the nested tags.

Converting to htmspecialchars will display the tags in a kind of source code view and also disable them. If you state on your site that HTML will be disabled people who try to post HTML etc. will just have to reckon ugly looking posts.

 
use the str_replace function!

$string = $_POST['formthing'];

$string2 = str_replace(&quot;</script> </script&quot;, &quot;&quot;, $string);

echo($string);

gd luck


Martin

Computing help and info:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top