简体   繁体   中英

Make strip_tags replace HTML with entities instead of nothing

I have this line of code:

$content = strip_tags($content, '<a><br><b><i>');

It works like it should, but I need for users to be able to enter code and for the code to be displayed on the web-page.
For example,

<?php
echo 'Hey';
?>

displays nothing. I need it to display

&lt;?php
echo 'Hey';
?&gt;

for example. What function should I use to do this?
EDIT: To clarify, the user sees the PHP code, it just gets replaced with HTML entities so the server doesn't try to run it.

Try:

$content = htmlspecialchars(strip_tags($content, '<a><br><b><i>'));

Or

$content = htmlentities(strip_tags($content, '<a><br><b><i>'), ENT_QUOTES);

See htmlspecialchars and htmlentities .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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