note 53200 deleted from function.substr by bobby

From: Date: Thu, 14 Sep 2006 17:16:09 +0000
Subject: note 53200 deleted from function.substr by bobby
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117169@lists.php.net to get a copy of this message
Note Submitter: bleakwind at msn dot com ---- This returns the portion of str specified by the start and length parameters.. It can performs multi-byte safe on number of characters. like mb_strcut() ... Note: 1.Use it like this bite_str(string str, int start, int length [,byte of on string]); 2.First character's position is 0. Second character position is 1, and so on... 3.$byte is one character length of your encoding, For example: utf-8 is "3", gb2312 and big5 is "2"...you can use the function strlen() get it... Enjoy it :) ... --- Bleakwind QQ:940641 http://www.weaverdream.com PS:I'm sorry my english is too poor... :( <?php // String intercept By Bleakwind At http://www.weaverdream.com // utf-8:$byte=3 | gb2312:$byte=2 | big5:$byte=2 function bite_str($string, $start, $len, $byte=3) { $str = ""; $count = 0; $str_len = strlen($string); for ($i=0; $i<$str_len; $i++) { if (($count+1-$start)>$len) { $str .= "..."; break; } elseif ((ord(substr($string,$i,1)) <= 128) && ($count < $start)) { $count++; } elseif ((ord(substr($string,$i,1)) > 128) && ($count < $start)) { $count = $count+2; $i = $i+$byte-1; } elseif ((ord(substr($string,$i,1)) <= 128) && ($count >= $start)) { $str .= substr($string,$i,1); $count++; } elseif ((ord(substr($string,$i,1)) > 128) && ($count >= $start)) { $str .= substr($string,$i,$byte); $count = $count+2; $i = $i+$byte-1; } } return $str; } // Test $str = "123456¶à×Ö½Ú123456×Ö·û´®123456½ØÈ¡º¯Êý"; for($i=0;$i<30;$i++){ echo "<br>".bite_str($str,$i,20); } ?>

« previous php.notes (#117169) next »