EllisLab text mark
Advanced Search
     
form_prep performance
Posted: 12 December 2007 10:10 AM   [ Ignore ]
Joined: 2007-12-12
1 posts
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;    
// 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?