简体   繁体   English

特殊字符的str_replace

[英]str_replace with special characters

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

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

When it echos out it strips the white_space but doesn't replace it with <SP> . 当它回显时,它将除去white_space,但不会将其替换为<SP> I'm guessing because of the < > . 我猜是因为< > Don't know how to fix this so it echos out <SP> in replace of the white_space? 不知道如何解决此问题,以便在替换white_space时回显<SP>吗?

If you see view-source you can see your replaced code but because HTML tries to parse it as something (eg. directive), you dont see it in parsed web view. 如果您看到视图源,则可以看到被替换的代码,但是由于HTML试图将其解析为某种内容(例如,指令),因此您在解析的Web视图中看不到它。

You can use html entities for exactly that reason. 正是由于这个原因,您可以使用html实体。

code would be: 代码将是:

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

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

You need to use htmlspecialchars() or htmlentities() function to make it display into &lt; 您需要使用htmlspecialchars()htmlentities()函数使其显示为&lt; and &gt; &gt; . Technically it shouldn't be <SP> , but instead &lt;SP&gt; 从技术上讲,它不应为<SP> ,而应为&lt;SP&gt; .

Change your code to: 将您的代码更改为:

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

Or you can do it in the first attempt, when you are trying to replace, as the correct format: 或者,您可以在尝试替换时以正确的格式进行第一次尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM