1 (edited by PunBB1.2 2009-11-14 13:20)

Topic: Question on functions

Functions:

//
// Equivalent to htmlspecialchars(), but allows &#[0-9]+ (for unicode)
//
function pun_htmlspecialchars($str)
{
    $str = preg_replace('/&(?!#[0-9]+;)/s', '&', $str);
    $str = str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $str);

    return $str;
}

//
// Equivalent to strlen(), but counts &#[0-9]+ as one character (for unicode)
//
function pun_strlen($str)
{
    return strlen(preg_replace('/&#([0-9]+);/', '!', $str));
}

Looking only at cases where the use UTF-8, I understand it correctly? Or even if use for example Win-1251, they still have meaning (such as if the text is sent in UTF-8) or both?

Tnx!

Re: Question on functions

This function will not work correct with UTF-8.