Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php echo html tag so it is viewable (not interpreted as code)

Tags:

html

php

echo

tags

I am currently trying to echo a text value from a variable which contains html-style tags. <...>

$string = "variable_name";
$tag_str = "<".$string.">";
echo $tag_str;

currently this echo's as nothing as it believes it is html code. How would I go about echoing <variable_name> to the page so it is viewable and not interpreted as code by the browser?

like image 939
Justice Avatar asked Jan 23 '14 17:01

Justice


1 Answers

You'll have to html encode your output

$string = "variable_name";
$tag_str = "<".$string.">";
echo htmlspecialchars($tag_str);
like image 165
Musa Avatar answered Sep 19 '22 06:09

Musa