如何在包含html的字符串中保留标记

mck*_*ght -1 php tags string converter

我有以下代码:

<?php
     $result = '<test>hello</test>';
     echo $result;
Run Code Online (Sandbox Code Playgroud)

当我执行它时,我只收到

hello
Run Code Online (Sandbox Code Playgroud)

看来是这样,当我试图将它删除时,PHP会删除字符串中的这些标记.如何在字符串中保留这些标签?

Nul*_*teя 11

你可以使用htmlentities()php功能:在你的情况下,

echo htmlentities($result);
Run Code Online (Sandbox Code Playgroud)

要么

echo htmlentities("<test>hello</test>");
Run Code Online (Sandbox Code Playgroud)

更好的方法将是

htmlspecialchars()
Run Code Online (Sandbox Code Playgroud)

  • 较少`htmlentities()`和moar`htmlspecialchars()`请. (2认同)