Preg_replace/str_replace() for changing `<` and `>` instances to `<` and `>` respectively

China☆狼群 提交于 2019-12-08 08:07:41

问题


One of our pages pulls content from a database table using the following code:

<?php echo $project['description']; ?>

What I need is for all &lt; and &gt; instances to be replaced with < and > respectively. Can you help modify the above code to include a preg_replace statement (or str_replace())?


回答1:


<?php echo htmlspecialchars_decode($project['description']); ?>

Should get you what you need.

If you are ONLY looking to decode those, though, then:

<?php echo str_replace("&lt;","<",str_replace("&gt;",">",$project['description'])); ?>

And preg_replace should look like this:

<?php echo preg_replace(&lt;,"<",preg_replace(&gt;,">",$project['description'])); ?>

I'm pretty sure the & isn't a special character, but if it does cause you issues, put a \ before it.



来源:https://stackoverflow.com/questions/9294270/preg-replace-str-replace-for-changing-lt-and-gt-instances-to-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!