PHP Function To Convert Html Entities Back To Ascii
Posted:
Friday, November 4th, 2011Last modified:
Sunday, March 29th, 2015This is a function to convert select HTML entities back to ASCII characters in PHP. Its initial purpose was undo the ENT_QUOTE setting in the Drupal 7 $head_title.
/** * Convert speficic HTML entities back to ASCII chars * * Consider the context and potential ramifications of converting any given HTML * entity back to ASCII, as this may introduce vulnerabilities. * * @param $str * The string to be manipulated. * * @param $more = FALSE * By default only ' and " are converted. Set to TRUE to convert an * extended set of HTML entities. */ function htmlEntitytoAscii($str=NULL, $more=FALSE) { $search = array(''', '"'); $replace = array("'", '"'); $str = str_replace($search, $replace, $str); if ($more) { // You can add whatever you want here. str_replace iterates through the // two arrays such that $replace[0] will replace all instances of // $search[0] and so on. $search = array('&'); $replace = array('&'); $str = str_replace($search, $replace, $str); } return $str; } ///// EXAMPLE USAGE ///// print 'View page source to see character transformations.'; print "
\n"; $string = '\' & "'; print 'Original: ' . $string; print "
\n"; $string = htmlspecialchars($string, ENT_QUOTES); print 'Converted to HTML entities: ' . $string; print "
\n"; print 'Back to ASCII: ' . htmlEntitytoAscii($string); print "
\n"; print 'Back to ASCII with $more=TRUE: ' . htmlEntitytoAscii($string, TRUE); print "
\n";
Available Wiki Topics
The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.