note 119517 added to function.mb-substr

From: Date: Mon, 27 Jun 2016 16:06:12 +0000
Subject: note 119517 added to function.mb-substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-205822@lists.php.net to get a copy of this message
you can make mb_substr working faster with long strings with usage of ucs-2 encoding. <?php header('Content-Type: text/html; charset=utf-8'); echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >'; function test($string, $encoding='utf8'){ $t1=microtime(true); $textlen=mb_strlen($string); $substr_len=3; for($i=0;$i<$textlen-$substr_len+1;$i++){ $substr=mb_substr($string,$i,$substr_len); } echo 'mb_substr, '.$encoding.': '.(microtime(true)-$t1); echo ' . check: '; if($encoding=='ucs2'){ $substr=mb_convert_encoding($substr,'utf-8','ucs2'); } var_dump( $substr ); echo ' . <br>'; echo '<br>'; } $corpus_short=str_repeat('тест Тест ',1000); // it works likewise slowly with "test Test" with utf8 mb_internal_encoding('utf-8'); test($corpus_short); $corpus_short_ucs2=mb_convert_encoding($corpus_short,'ucs2','utf-8'); mb_internal_encoding('ucs2'); test($corpus_short_ucs2,'ucs2'); ?> output: mb_substr, utf8: 0.26480984687805 . check: string(5) "ст " . mb_substr, ucs2: 0.0048871040344238 . check: string(5) "ст " . ---- Server IP: 72.52.91.14 Probable Submitter: 78.138.176.205 ---- Manual Page -- http://php.net/manual/en/function.mb-substr.php Edit -- https://master.php.net/note/edit/119517 Del: integrated -- https://master.php.net/note/delete/119517/integrated Del: useless -- https://master.php.net/note/delete/119517/useless Del: bad code -- https://master.php.net/note/delete/119517/bad+code Del: spam -- https://master.php.net/note/delete/119517/spam Del: non-english -- https://master.php.net/note/delete/119517/non-english Del: in docs -- https://master.php.net/note/delete/119517/in+docs Del: other reasons-- https://master.php.net/note/delete/119517 Reject -- https://master.php.net/note/reject/119517 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205822) next »