note 119503 added to function.mb-substr

From: Date: Thu, 23 Jun 2016 15:02:26 +0000
Subject: note 119503 added to function.mb-substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-205800@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. i have written similar thing in mb_convert_encoding page several years ago, and qbolec has written explanation and a solution about this topic in this page. <?php header('Content-Type: text/html; charset=utf-8'); echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >'; $corpus_short=str_repeat('тест Тест ',1000); mb_internal_encoding('utf-8'); $t1=microtime(true); $textlen=mb_strlen($corpus_short); $substr_len=3; for($i=0;$i<$textlen-$substr_len+1;$i++){ $substr=mb_substr($corpus_short,$i,$substr_len); } echo 'mb_substr, utf8: '.(microtime(true)-$t1); echo '<br>'; $corpus_short_utf16=mb_convert_encoding($corpus_short,'utf-16','utf-8'); mb_internal_encoding('utf-16'); $t1=microtime(true); $textlen=mb_strlen($corpus_short_utf16); $substr_len=3; for($i=0;$i<$textlen-$substr_len+1;$i++){ $substr=mb_substr($corpus_short_utf16,$i,$substr_len); } echo 'mb_substr, utf16: '.(microtime(true)-$t1); echo '<br>'; $corpus_short_ucs2=mb_convert_encoding($corpus_short,'ucs2','utf-8'); mb_internal_encoding('ucs2'); $t1=microtime(true); $textlen=mb_strlen($corpus_short_ucs2); $substr_len=3; for($i=0;$i<$textlen-$substr_len+1;$i++){ $substr=mb_substr($corpus_short_ucs2,$i,$substr_len); } echo 'mb_substr, ucs2: '.(microtime(true)-$t1); ?> output: mb_substr, utf8: 0.26405191421509 mb_substr, utf16: 1.0428597927094 mb_substr, ucs2: 0.0046970844268799 ---- 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/119503 Del: integrated -- https://master.php.net/note/delete/119503/integrated Del: useless -- https://master.php.net/note/delete/119503/useless Del: bad code -- https://master.php.net/note/delete/119503/bad+code Del: spam -- https://master.php.net/note/delete/119503/spam Del: non-english -- https://master.php.net/note/delete/119503/non-english Del: in docs -- https://master.php.net/note/delete/119503/in+docs Del: other reasons-- https://master.php.net/note/delete/119503 Reject -- https://master.php.net/note/reject/119503 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205800) next »