note 64486 added to function.substr

From: Date: Sat, 15 Apr 2006 18:34:04 +0000
Subject: note 64486 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-108184@lists.php.net to get a copy of this message
<? function utf8_substr($str, $start, $length=null){ # # UTF8_SUBSTR() # www.yeap.lv # if(!defined('UTF8_NOMBSTRING') && function_exists('mb_substr')){ return mb_substr($str,$start,$length,'utf-8'); } $unicode = array(); $values = array(); $lookingFor = 1; for ($i = 0; $i < strlen( $str ); $i++ ) { $thisValue = ord( $str[ $i ] ); if ( $thisValue < 128 ) $unicode[] = $thisValue; else { if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3; $values[] = $thisValue; if ( count( $values ) == $lookingFor ) { $number = ( $lookingFor == 3 ) ? ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ): ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 ); $unicode[] = $number; $values = array(); $lookingFor = 1; } } } $str = array_slice($unicode,$start,$length); foreach( $str as $unicode ) { if ( $unicode < 128 ) { $utf8.= chr( $unicode ); } elseif ( $unicode < 2048 ) { $utf8.= chr( 192 + ( ( $unicode - ( $unicode % 64 ) ) / 64 ) ); $utf8.= chr( 128 + ( $unicode % 64 ) ); } else { $utf8.= chr( 224 + ( ( $unicode - ( $unicode % 4096 ) ) / 4096 ) ); $utf8.= chr( 128 + ( ( ( $unicode % 4096 ) - ( $unicode % 64 ) ) / 64 ) ); $utf8.= chr( 128 + ( $unicode % 64 ) ); } } return $utf8; } ?> ---- Server IP: 81.94.227.69 Probable Submitter: 217.195.57.149 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- http://master.php.net/note/edit/64486 Del: integrated -- http://master.php.net/note/delete/64486/integrated Del: useless -- http://master.php.net/note/delete/64486/useless Del: bad code -- http://master.php.net/note/delete/64486/bad+code Del: spam -- http://master.php.net/note/delete/64486/spam Del: non-english -- http://master.php.net/note/delete/64486/non-english Del: in docs -- http://master.php.net/note/delete/64486/in+docs Del: other reasons-- http://master.php.net/note/delete/64486 Reject -- http://master.php.net/note/reject/64486 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#108184) next »