PHP - How to echo code as text

Now I have a request containing HTML code, and when I select it in php, it makes code,

$query = mysql_query("SELECT * FROM `table`")or die(mysql_error()); while($arr = mysql_fetch_array($query)){ $num = mysql_num_rows($query); $code = $arr ['code']; echo $code; } 

and this request has this code <a href="http://google.com">Click Here</a>

when he repeats, he shows me. Click here, but I want it to sow me code.

So how can I do this in PHP.

Thanks Klaus

+4
source share
4 answers

Use htmlspecialchars() to prevent the browser from interpreting HTML elements.

 $query = mysql_query("SELECT * FROM `table`")or die(mysql_error()); while($arr = mysql_fetch_array($query)){ $num = mysql_num_rows($query); $code = $arr ['code']; echo htmlspecialchars($code); } 
+5
source

Just encode the output with htmlspecialchars

 $query = mysql_query("SELECT * FROM `table`")or die(mysql_error()); while($arr = mysql_fetch_array($query)){ $num = mysql_num_rows($query); $code = $arr ['code']; echo htmlspecialchars($code); } 
+4
source

You can use htmlentities :

 echo htmlentities($code); 

This function is identical to htmlspecialchars () in all ways, except with htmlentities (), all characters that have an HTML character object equivalents are translated into these objects.

+2
source

Just use: -

 echo htmlentities($code); 

The htmlentities() function takes a string and returns the same string with HTML converted to HTML objects.

This prevents the browser from using it as an HTML element, and it prevents code from being run if you want to display some user data on your website.

+1
source

Source: https://habr.com/ru/post/1488966/


All Articles