While making a post feature with PHP , i realized that anyone can post their text with a tag also. For ex. <b> What's Up</b>
or even <marquee>Look, moving</marquee>
. When these types of data will be printed it would affect inside the post. Like <b>
will make the post bold. There must a html tag
or a way with PHP
or Javascript
to disable html inside a tag.
I'm well aware of strip_tags()
, but this function strips the tags for ex. <b>blah blah</b>
will be shown like blah blah
but i want it to be shown <b> blah blah<b>
but not the HTML
effect it would cause.
Asked
Active
Viewed 266 times
-1

Mihir Ujjainwal
- 140
- 2
- 13
-
1Possible duplicate of [HTML,PHP - Escape '<' and '>' symbols while echoing](http://stackoverflow.com/questions/10551116/html-php-escape-and-symbols-while-echoing) – xmojmr Feb 20 '15 at 20:28
2 Answers
1
Use the strip-tags function. Something like:
<?php
strip_tags($content, '');
?>
The second parameter is for allowed tags.
To show the html but not run it use <pre>
tags and CDATA
:
<pre><![CDATA[
<b>Blah Blah</b>
]]></pre>
Unfortunately, this only works for XHTML
and XML
.
Another alternative. and probably the most viable, is <xmp>
: http://jsfiddle.net/8yfghmL9/

Community
- 1
- 1

Peter Noble
- 675
- 10
- 21
-
-
1) Is it necessary to use `
` tag with `<![CDATA[ ]>` 2)is `
– Mihir Ujjainwal Feb 20 '15 at 20:11` more compatible ? 3) Does ` ` has it's own style, like ` ` or `
` tags does.
-
Using `
` tags in certain browsers is necessary. However this still works only with `XHTML`. `
– Peter Noble Feb 20 '15 at 20:13` however is supported in most modern browsers. So, yes, I would go with ` `. -
-
No. It's basic text. You can enclose it all in a `div` and style it anyway you want. – Peter Noble Feb 20 '15 at 20:17
0
you can use
<?php
echo strip_tags("Hello <b>world!</b>");
?>
this will strip the tags.. and give you pure text ...

Mohamad Shiralizadeh
- 8,329
- 6
- 58
- 93

Vishal Wadhawan
- 1,085
- 1
- 9
- 11