带有特殊字符的str_replace

时间:2017-04-18 18:08:00

标签: php character echo str-replace

O_CREAT

当它回声时它会剥离white_space但不会用--wS--S--T替换它。我因为$fetched_column['title'] = str_replace(' ', '<SP>', $fetched_column['title']); echo $fetched_column['title'] . '<br>'; <SP>而猜测。不知道如何解决这个问题,以便在替换white_space时回复<

2 个答案:

答案 0 :(得分:0)

如果您看到view-source,您可以看到替换的代码,但是因为HTML尝试将其解析为某些内容(例如.profile),您在解析的Web视图中看不到它。

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

代码将是:

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

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

答案 1 :(得分:0)

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

将您的代码更改为:

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

或者,当您尝试替换时,您可以在第一次尝试时将其作为正确的格式:

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