str_replace 有特殊字符

str_replace with special characters

$fetched_column['title'] = str_replace(' ', '<SP>', $fetched_column['title']);

echo $fetched_column['title'] . '<br>';

当它回显时,它会删除 white_space 但不会用 <SP> 替换它。我猜是因为 < >。不知道如何解决这个问题,所以它用 <SP> 代替 white_space?

如果您看到查看源代码,您可以看到您替换的代码,但是因为 HTML 试图将它解析为某种东西(例如指令),您在解析的 Web 视图中看不到它。

正是出于这个原因,您可以使用 html 个实体。

代码为:

$fetched_column['title'] = str_replace(' ', '&lt;SP&gt;', $fetched_column['title']);

echo $fetched_column['title'] . '<br>';

您需要使用htmlspecialchars() or htmlentities()函数使其显示为&lt;&gt;。从技术上讲,它不应该是 <SP>,而是 &lt;SP&gt;.

将您的代码更改为:

echo htmlentities($fetched_column['title']) . '<br>';

或者你可以在第一次尝试的时候做,当你尝试替换时,作为正确的格式:

$fetched_column['title'] = str_replace(' ', '&lt;SP&gt;', $fetched_column['title']);