Preg_replace/str_replace() for changing `<` and

2019-06-05 14:12发布

问题:

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.