I plan to use my custom function below while getting from data from my mysql table & print it as html. Since htmlspecialchars() translate tags to html entities, I retranslate them ( p, br, strong) to tags.
My question is: Is it efficient enough or Is there any other shorter or more efficient way to achieve this aim? If you know any, can you please guide me with at least keywords? I can look fort he details in php.net and this site.
Thanks, regards
function safe_output_from_mysql($safe_echo_to_html)
{
$safe_echo_to_html = mb_convert_encoding($safe_echo_to_html, 'UTF-8', mb_detect_encoding($safe_echo_to_html));
$safe_safe_echo_to_html = htmlspecialchars($safe_echo_to_html, ENT_QUOTES, "UTF-8");
$safe_echo_to_html = preg_replace("<br />","<br />",$safe_echo_to_html);
$safe_echo_to_html = preg_replace("<p>","<p>",$safe_echo_to_html);
$safe_echo_to_html = preg_replace("</p>","</p>",$safe_echo_to_html);
$safe_echo_to_html = preg_replace("<strong>","<strong>",$safe_echo_to_html);
$safe_echo_to_html = preg_replace("</strong>","</strong>",$safe_echo_to_html);
return $safe_echo_to_html;
}
There is no need to call preg_replace() multiple times. You can use a single pattern to match all the desired tags:
I’m assuming, of course, that you’re actually planning to use regex to do the match. If the search strings are straight text, then strtr() is more efficient.