system/helpers/xml_helper.php - line 37
BEFORE: 10000 iterations take about 0.4759 seconds
function xml_convert($str)
{
$temp = '__TEMP_AMPERSANDS__';
// Replace entities to temporary markers so that
// ampersands won't get messed up
$str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
$str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
$str = str_replace(array("&","<",">","\"", "'", "-"),
array("&", "& lt;", "& gt;", """, "'", "-"),
$str);
// Decode the temp markers back to entities
$str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
$str = preg_replace("/$temp(\w+);/","&\\1;", $str);
return $str;
}
AFTER: 10000 iterations take about 0.1962 seconds
function xml_convert_geert($str) {
// Convert ampersands to entities only if they're not part of an existing entity
$str = preg_replace('/&(?!(#\d+|[a-z]+);)/i', '&', $str);
// Convert: < > ' " -
$str = str_replace(
array('<', '>', '\'', '"', '-'),
array('& lt;', '& gt;', ''', '"', '-'),
$str
);
return $str;
}
Note that the spaces in & lt; and & gt; should be removed. If I didn’t space these entities out, the forum converts them to the actual < and > characters.
My optimized version of this function is a lot faster and cleaner, right?
Please copy this optimization to the following locations as well:
- system/helpers/form_helper.php > form_prep() - line 340
- system/libraries/Trackback.php > convert_xml() - line 406
