SapAuthor Posted August 10, 2009 Share Posted August 10, 2009 I've been coding with PHP for a while now, and when creating dynamic drop down boxes and more, i ALWAYS run into the problem of ' and " or other symbols being put in by users (like < > or others) and messing up the html structure. I know you can do a str_replace with an array to swap out certain characters with an alternative (like html's & q uot;) However, I got to know, what is the best way to go about this? I have many pages, that have to do this. Is there a PHP function that will take text and replace any characters it can with the html &____;, or do people make up their own array and copy it onto every single page? Searched but couldn't find much. You would think PHP would have put in a function to do this function since it seems like an issue that would come up a lot. Thanks for any help! Sincerely, Trevor Quote Link to comment https://forums.phpfreaks.com/topic/169693-solved-best-way-to-convert-text-to-html-compliant-code/ Share on other sites More sharing options...
sford999 Posted August 10, 2009 Share Posted August 10, 2009 http://us2.php.net/manual/en/function.htmlspecialchars.php or http://us2.php.net/manual/en/function.html-entity-decode.php Quote Link to comment https://forums.phpfreaks.com/topic/169693-solved-best-way-to-convert-text-to-html-compliant-code/#findComment-895183 Share on other sites More sharing options...
oni-kun Posted August 10, 2009 Share Posted August 10, 2009 Seems you have a simple solution to this. <?php $str = "A 'quote' is <b>bold</b>"; // Outputs: A 'quote' is <b>bold</b> echo htmlentities($str); // Outputs: A 'quote' is <b>bold</b> echo htmlentities($str, ENT_QUOTES); ?> Htmlentities can also support encoding.. ($str, ENT_QUOTES, 'UTF-8').. More here: http://us2.php.net/manual/en/function.htmlentities.php htmlspecialchars is more simple but doesn't HTML encode everything, which in your case may not be as wise. Quote Link to comment https://forums.phpfreaks.com/topic/169693-solved-best-way-to-convert-text-to-html-compliant-code/#findComment-895184 Share on other sites More sharing options...
SapAuthor Posted August 10, 2009 Author Share Posted August 10, 2009 Gosh, i'm such a moron rofl. After finding out they were called html entities, finally some searches came up, and the stupid thing is i've seen that function before, just forgot about it. Thanks for the quick help all! Quote Link to comment https://forums.phpfreaks.com/topic/169693-solved-best-way-to-convert-text-to-html-compliant-code/#findComment-895186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.