CodeIgniter Forums
form_prep performance - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: form_prep performance (/thread-4718.html)



form_prep performance - El Forum - 12-12-2007

[eluser]Unknown[/eluser]
Code:
function form_prep($str = '')
{
    if ($str === '')
    {
        return '';
    }

    $temp = '__TEMP_AMPERSANDS__';
    
    // Replace entities to temporary markers so that
    // htmlspecialchars won't mess them up
    $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
    $str = preg_replace("/&(\w+);/",  "$temp\\1;", $str);

    $str = htmlspecialchars($str);

    // In case htmlspecialchars misses these.
    $str = str_replace(array("'", '"'), array("'", """), $str);    
    
    // Decode the temp markers back to entities
    $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
    $str = preg_replace("/$temp(\w+);/","&\\1;",$str);    
    
    return $str;    
}


Code:
// Replace entities to temporary markers so that
// htmlspecialchars won't mess them up
$str = preg_replace("/&(?!\#[0-9]+;)/si", "&", $str);

Any best solution about it?