1

I wanted to disable HTML in a div simply because I want just a plain text to be shown inside a div.

If you retrieve a data from the DB and it's value is <b> Good! </b> which is equivalent to Good! in HTML. Now what I want is that, I wanted to show the word <b> Good! </b> (without html style) and not the Good! (with html style). Any method would do, whether through javascript or jquery as long as it plays the same role.

Example:

$value = "`<b> Good </b>`";

<div id = "true"><?php $value; ?></div>

And the div would simply have an output like this: <b> Good </b>

How could I do that? Any help would be much appreciated. Thanks

1

8 Answers 8

2

Use This PHP functoin htmlspecialchars() example:

$value = "`<b> Good </b>`";

<div id = "true"><?php echo htmlspecialchars($value); ?></div>
4
  • Is it possible to except the element <a> from being disabled? Cause I want my link to stay as it is. And not being printed as <a href = "#"> this Link </a>
    – Makudex
    Commented Sep 12, 2015 at 15:01
  • try this it will help you: <?php $string = '<b>abc</b> <a href="www.google.com">google</a>'; $string=htmlspecialchars($string); $patterns = array("/&lt;a/","/&lt;\/a&gt;/","/&quot;&gt;/"); $replacements = array('<a','</a>','</a>'); $expected_result = preg_replace($patterns, $replacements, $string); echo $expected_result; ?> Commented Sep 12, 2015 at 16:10
  • I'ts almost the answer but there are some extra characters. From www.google.com, this was the output: "www.google.com</a Any idea?
    – Makudex
    Commented Sep 13, 2015 at 3:15
  • Hope This will work: <?php $string = '<b>abc</b> <a href="google.com">google</a>'; $string=htmlspecialchars($string); $patterns = array("/&lt;\/a&gt;/","/&gt;/","/&lt;a/","/&quot;/"); $replacements = array('</a>',' >','<a ',''); $expected_result = preg_replace($patterns, $replacements, $string); echo $expected_result; ?> Commented Sep 13, 2015 at 16:25
1

I would suggest simple solution to this:

&lt = less than
&rt = greater than

&lttag&gt Hello i am simple text not an HTML element &lt/tag&gt
0

You can use jquery text() method like this: $("content").text()

Reference: http://api.jquery.com/text/

0

You should convert the html-tags to html-tags. E.g. &lt;b&gt;Good!&lt;/b&gt;

How this is done depends on the programming language your are using.

0

Simplest solution would be to add it to a Dom element then return the text.

Something like this.

Var div = document.createElement('div');

Div.innerhtml = ' good';

Var res = div.innerText;

In this case res would be equal to 'good'.

0

Just try the jQuery text() method.

$('yourdiv').text(<b> Good! </b>);

Know more from here

0

Tried utilizing textarea element ?

$("textarea").html("<b> Good! </b>")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<textarea></textarea>

0

What you're attempting, is to whitelist some html tags in the input. See for example the answers to this question for how to do it in php.

Not the answer you're looking for? Browse other questions tagged or ask your own question.