note 32528 deleted from ref.strings by tomsommer
| From: | tomsommer@php.net | Date: | Wed, 15 Sep 2004 20:47:47 +0000 |
| Subject: | note 32528 deleted from ref.strings by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76787@lists.php.net to get a copy of this message | ||
Note Submitter: yuelinniao at bbs dot nju dot edu dot cn
----
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;
}
}