note 32528 added to ref.strings

From: Date: Fri, 30 May 2003 08:32:04 +0000
Subject: note 32528 added to ref.strings
Groups: php.notes 
Request: Send a blank email to php-notes+get-49404@lists.php.net to get a copy of this message
I wrote this function to cut a mixed string(there are English words/numbers/Chinese characters in it) into short strings. e.g. dddddeeeeebbbbbggggg ---cut---> ddddd eeeee bbbbb ggggg You know, a Chinese character is made up of two bits, if divide the two bits, the Chinese character can not be recognized. So I make a judgement before cutting. Hope this will help someone. /* eg words_cut($string, 0, 5); */ function words_cut($string, $begin, $shortlength, $number=1) { $length = strlen($string); if($length > $shortlength * $number) { $end = $begin + $shortlength; $flag = 0; for($x=$begin; $x < $end; $x++) { if(ord($string[$x]) <= 127) { $flag++; } } if($flag%2==1) { $end++; } $first_part = substr($string, 0, $end); $last_part = substr($string, $end); $newstring = $first_part. "\n" .$last_part; $number++; return words_cut($newstring, $end, $shortlength, $number); } else { return $string; } } ---- Manual Page -- http://www.php.net/manual/en/ref.strings.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32528 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32528&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32528&report=yes

« previous php.notes (#49404) next »