note 86824 added to function.mb-split
| From: | giedriustum at gmail dot com | Date: | Wed, 05 Nov 2008 09:45:41 +0000 |
| Subject: | note 86824 added to function.mb-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146155@lists.php.net to get a copy of this message | ||
The code
function mbStringToArray ($string) {
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string,0,1,"UTF-8");
$string = mb_substr($string,1,$strlen,"UTF-8");
$strlen = mb_strlen($string);
}
return $array;
}
may be a bit inefficient cuz this asignment :
$string = mb_substr($string,1,$strlen,"UTF-8");
in cycle is worth n-i operations, where n - is initial string length and i - the current cycle
number.
Total complexity of the algorithm becomes O(n*n / 2).
I would like propose following algorithm:
mb_regex_encoding('UTF-8');
mb_internal_encoding('UTF-8' );
$strArray = array();
for ( $i=0; $i < mb_strlen($str); $i++ ) {
$strArray[] = mb_substr($str, $i, 1) ;
}
Now it depends on the complexity of the mb_substr($str, $i, 1).
If it is constant, then the whole algorithm complexity becomes O(n)
----
Server IP: 82.135.230.5
Probable Submitter: 82.135.192.113
----
Manual Page -- http://www.php.net/manual/en/function.mb-split.php
Edit -- https://master.php.net/note/edit/86824
Del: integrated -- https://master.php.net/note/delete/86824/integrated
Del: useless -- https://master.php.net/note/delete/86824/useless
Del: bad code -- https://master.php.net/note/delete/86824/bad+code
Del: spam -- https://master.php.net/note/delete/86824/spam
Del: non-english -- https://master.php.net/note/delete/86824/non-english
Del: in docs -- https://master.php.net/note/delete/86824/in+docs
Del: other reasons-- https://master.php.net/note/delete/86824
Reject -- https://master.php.net/note/reject/86824
Search -- https://master.php.net/manage/user-notes.php