6

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?

Justice
  • 169
  • 1
  • 2
  • 9

3 Answers3

8

You'll have to html encode your output

$string = "variable_name";
$tag_str = "<".$string.">";
echo htmlspecialchars($tag_str);
Musa
  • 96,336
  • 17
  • 118
  • 137
1

The angle brackets (<>) are precisely what tells the browser that it should be treated as HTML code. Instead, output the HTML-encoded versions of those otherwise special characters:

$tag_str = "&lt;".$string."&gt;";

Alternatively, automate this process:

$tag_str = htmlspecialchars("<".$string.">");
David
  • 208,112
  • 36
  • 198
  • 279
0

Use highlight_string().See below code

$string = "variable_name";
$tag_str = "<".$string.">";
highlight_string($tag_str);
Zword
  • 6,605
  • 3
  • 27
  • 52