<html>
<head>

    <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>
    <style>
    <!--

    #org {
        border-width: 1px;
        border-color: #808080;
        border-style: dotted;

        background-color: #efefef;
        padding: 10px;
        margin-bottom: 40px;
    }

    #after {
        border-width: 1px;
        border-color: #808080;
        border-style: dashed;

        background-color: #efefef;
        padding: 10px;
        margin-bottom: 40px;
    }
    
-->
</style>
</head>

<body>

<form method="post" action="<?=$PHP_SELF?>">
<textarea name='text' cols='40' rows='10'><?=$_POST[text]?></textarea>
<input type=submit>
</form>

<br><br><br>

<?PHP

function utf8_validation$str, &$i ){

    
$i 0;
    
$len strlen($str);

    while( 
$i $len ){
    
        if( (
ord($str[$i]) & 0xF0) == 0xE0 ){
            if( 
$i <= ($len-3) && 
                (
ord($str[$i+1])&0x80 == 0x80) &&
                (
ord($str[$i+2])&0x80 == 0x80) )
                    
$i += 3;
            else
                return;
        }
        
// 2Byte
        
else if( (ord($str[$i]) & 0xE0) == 0xC0 ){
            if( 
$i <= ($len-2) && 
                (
ord($str[$i+1])&0x80 == 0x80) )
                    
$i += 2;
            else
                return;
        }
        
// 1Byte
        
else {
            
$i++;
        }
        
    }
    
}

function 
utf8_get_next_char$str, &$i ){

    
//echo bin2hex($str[$i])." ";
    
    // 3Byte 

    
if( (ord($str[$i]) & 0xF0) == 0xE0 ){
        
$i += 3;
        return 
substr$str, ($i-3), );
    }
    
// 2Byte
    
else if( (ord($str[$i]) & 0xE0) == 0xC0 ){
        
$i += 2;
        return 
substr$str, ($i-2), );
    }
    
// 1Byte
    
else {
        
$i++;
        return 
$str[$i-1];
    }
    return 
NULL;
     
}

if( 
$_POST[text] ){

    
$i 0;
    
$text "";
    while( (
$ch utf8_get_next_char$_POST[text], $i )) != NULL ){

        
$len 0;
        if( 
$ch == '\n' ){
            
$ch "Newline";
            
$len 1;
        }
        else if( 
$ch == '\t' ){
            
$ch "Tab";
            
$len 1;
        }
        else if( 
$ch == '\r' ){
            
$ch "CarrageReturn";
            
$len 1;
        }
        else
            
$len strlen($ch);

        
$text .= $ch.": ".$len."Bytes <BR />";

    }

    echo 
"<div id='org'>\n";
    echo 
"<H3>Original is</H3>";
    echo 
str_replace"\n""<br>\n"$_POST[text] );
    echo 
"</div>\n";

    echo 
"<div id='after'>\n";
    echo 
"<H3>After autolink...</H3>\n";
    echo 
"<tt>".$text ."</tt>";
    echo 
"</div>\n";
}

show_source(__FILE__);

?>

</body>

</html>