a class to split mixed string with double byte character and ascii chareater to lines
<?php
header("Content-type: image/jpeg");
/*<example>*/
$output = new outputstr();
$output->str = "一二三四五w六七八九十一二三aa四五六七八九十一r二三 四五六七八九十一二三四五六七八九十";
$output->nextlen = 27;
$output->mainfunc();
print_r($output->splitstr);
/*</example>*/
/**<class outputstr>
* this class is to split the string which was fixed with unicode character and ascii character
* as lines array by defined line length for other function to use.
*
* run request:the original str which will be splited must be encoded with utf8,if not ,nothing can be get.
*
* $this->splitstr is the array to store lines we want at last.
* @auther :bailing wu <gucdai@yahoo.com.cn>
*/
class outputstr{
/**
* @param str ,store the whole str which must be output as lines.
*/
var $str = "";
/**
* @param len,store the whole str length.
*/
var $len = "";
/**
* @param lines,output lines number.
*/
var $lines = "";
/**
* @param nextpos,next line start position.
*/ //echo "行数:$lines \n";
var $nextpos = 0;
/**
* @param nextlen,use the character number to limit the width of every line
*/
var $nextlen = 27;
/**
* @param splitstr,this param is to store the lines array of the str.
*/
var $splitstr = array();
/*<outputstr>*/
function outputstr(){
$this->len = strlen($this->str);
$this->nextpos = 0;
$this->lines = intval($this->len/$this->nextlen)+1; //output lines number.
}
/*</outputstr>*/
/*<mainfunc>*/
function mainfunc(){
/*<for>*/
for($i=1;$i<=$this->lines;$i++){
//echo "nextpos:$this->nextpos = nextlen: $this->nextlen \n";
$currentpos = $this->nextpos;
$this->nextpos = $this->getPos($currentpos);
$currentlen = $this->nextpos - $currentpos;
$substr = substr($this->str,$currentpos,$currentlen);
$this->splitstr[] = $substr;
//eval($func);
//echo "$substr \n";
}/*</for>*/
}
/*</mainfunc>*/
/**
* this function get next position for substr.
* @param str ,the whole initial string which must be output by lines.
* @param currentpos,to get current operately string.
* @param nextlen,limit length of the current operately string.
* @return nextpos.
*/
function getPos($currentpos){
$tmp = substr($this->str,$currentpos,$this->nextlen);
$rawtextarray = preg_split("//",$tmp,-1, PREG_SPLIT_NO_EMPTY);
$rawtext = array();
//echo "\n tmp:$tmp \n";die;
for($i=0;$i<count($rawtextarray);$i++)$rawtext[] = ord($rawtextarray[$i]); //ord(char)
$rawtextlen = $this->nextlen;
//<for***********>
for($i=0;$i<$rawtextlen;$i++){
//<if>
if ($rawtext[$i] < 0x80) { // One byte
$asciibytes++; // Ignore ASCII, can throw off count
}
else if (0xC0 <= $rawtext[$i] && $rawtext[$i] <= 0xDF && // Two bytes
$i+1 < $rawtextlen && 0x80 <= $rawtext[$i+1] && $rawtext[$i+1] <= 0xBF) {
$goodbytes += 2; $i++;
}
else if (0xE0 <= $rawtext[$i] && $rawtext[$i] <= 0xEF && // Three bytes
$i+2 < $rawtextlen && 0x80 <= $rawtext[$i+1] && $rawtext[$i+1] <= 0xBF &&
0x80 <= $rawtext[$i+2] && $rawtext[$i+2] <= 0xBF) {
$goodbytes += 3; $i+=2;
}//</if>
}//</for*********>
//echo "ascii字节数:$asciibytes \n";
if($asciibytes > 0){
$offset =3-($asciibytes%3);
$nextpos = $currentpos + $this->nextlen - $offset ;
}
else{
$nextpos = $currentpos + $this->nextlen;
}
return($nextpos);
}
}
/*</class outputstr>*/
/***down is backuped**/
/**
* this function get next position for substr.
* @param str ,the whole initial string which must be output by lines.
* @param currentpos,to get current operately string.
* @param currentlen,limit length of the current operately string.
* @return nextpos.
*/
function getPos(&$str,$currentpos,$nextlen){
$tmp = substr($str,$currentpos,$nextlen);
$rawtextarray = preg_split("//",$tmp,-1, PREG_SPLIT_NO_EMPTY);
$rawtext = array();
for($i=0;$i<count($rawtextarray);$i++)$rawtext[] = ord($rawtextarray[$i]); //ord(char)
$rawtextlen = $nextlen;
//<for***********>
for($i=0;$i<$rawtextlen;$i++){
//<if>
if ($rawtext[$i] < 0x80) { // One byte
$asciibytes++; // Ignore ASCII, can throw off count
}
else if (0xC0 <= $rawtext[$i] && $rawtext[$i] <= 0xDF && // Two bytes
$i+1 < $rawtextlen && 0x80 <= $rawtext[$i+1] && $rawtext[$i+1] <= 0xBF) {
$goodbytes += 2; $i++;
}
else if (0xE0 <= $rawtext[$i] && $rawtext[$i] <= 0xEF && // Three bytes
$i+2 < $rawtextlen && 0x80 <= $rawtext[$i+1] && $rawtext[$i+1] <= 0xBF &&
0x80 <= $rawtext[$i+2] && $rawtext[$i+2] <= 0xBF) {
$goodbytes += 3; $i+=2;
}//</if>
}//</for*********>
//echo "ascii字节数:$asciibytes \n";
if($asciibytes > 0){
$offset =3-($asciibytes%3);
$nextpos = $currentpos + $nextlen - $offset ;
}
else{
$nextpos = $currentpos + $nextlen;
}
return($nextpos);
}
?>
header("Content-type: image/jpeg");
/*<example>*/
$output = new outputstr();
$output->str = "一二三四五w六七八九十一二三aa四五六七八九十一r二三 四五六七八九十一二三四五六七八九十";
$output->nextlen = 27;
$output->mainfunc();
print_r($output->splitstr);
/*</example>*/
/**<class outputstr>
* this class is to split the string which was fixed with unicode character and ascii character
* as lines array by defined line length for other function to use.
*
* run request:the original str which will be splited must be encoded with utf8,if not ,nothing can be get.
*
* $this->splitstr is the array to store lines we want at last.
* @auther :bailing wu <gucdai@yahoo.com.cn>
*/
class outputstr{
/**
* @param str ,store the whole str which must be output as lines.
*/
var $str = "";
/**
* @param len,store the whole str length.
*/
var $len = "";
/**
* @param lines,output lines number.
*/
var $lines = "";
/**
* @param nextpos,next line start position.
*/ //echo "行数:$lines \n";
var $nextpos = 0;
/**
* @param nextlen,use the character number to limit the width of every line
*/
var $nextlen = 27;
/**
* @param splitstr,this param is to store the lines array of the str.
*/
var $splitstr = array();
/*<outputstr>*/
function outputstr(){
$this->len = strlen($this->str);
$this->nextpos = 0;
$this->lines = intval($this->len/$this->nextlen)+1; //output lines number.
}
/*</outputstr>*/
/*<mainfunc>*/
function mainfunc(){
/*<for>*/
for($i=1;$i<=$this->lines;$i++){
//echo "nextpos:$this->nextpos = nextlen: $this->nextlen \n";
$currentpos = $this->nextpos;
$this->nextpos = $this->getPos($currentpos);
$currentlen = $this->nextpos - $currentpos;
$substr = substr($this->str,$currentpos,$currentlen);
$this->splitstr[] = $substr;
//eval($func);
//echo "$substr \n";
}/*</for>*/
}
/*</mainfunc>*/
/**
* this function get next position for substr.
* @param str ,the whole initial string which must be output by lines.
* @param currentpos,to get current operately string.
* @param nextlen,limit length of the current operately string.
* @return nextpos.
*/
function getPos($currentpos){
$tmp = substr($this->str,$currentpos,$this->nextlen);
$rawtextarray = preg_split("//",$tmp,-1, PREG_SPLIT_NO_EMPTY);
$rawtext = array();
//echo "\n tmp:$tmp \n";die;
for($i=0;$i<count($rawtextarray);$i++)$rawtext[] = ord($rawtextarray[$i]); //ord(char)
$rawtextlen = $this->nextlen;
//<for***********>
for($i=0;$i<$rawtextlen;$i++){
//<if>
if ($rawtext[$i] < 0x80) { // One byte
$asciibytes++; // Ignore ASCII, can throw off count
}
else if (0xC0 <= $rawtext[$i] && $rawtext[$i] <= 0xDF && // Two bytes
$i+1 < $rawtextlen && 0x80 <= $rawtext[$i+1] && $rawtext[$i+1] <= 0xBF) {
$goodbytes += 2; $i++;
}
else if (0xE0 <= $rawtext[$i] && $rawtext[$i] <= 0xEF && // Three bytes
$i+2 < $rawtextlen && 0x80 <= $rawtext[$i+1] && $rawtext[$i+1] <= 0xBF &&
0x80 <= $rawtext[$i+2] && $rawtext[$i+2] <= 0xBF) {
$goodbytes += 3; $i+=2;
}//</if>
}//</for*********>
//echo "ascii字节数:$asciibytes \n";
if($asciibytes > 0){
$offset =3-($asciibytes%3);
$nextpos = $currentpos + $this->nextlen - $offset ;
}
else{
$nextpos = $currentpos + $this->nextlen;
}
return($nextpos);
}
}
/*</class outputstr>*/
/***down is backuped**/
/**
* this function get next position for substr.
* @param str ,the whole initial string which must be output by lines.
* @param currentpos,to get current operately string.
* @param currentlen,limit length of the current operately string.
* @return nextpos.
*/
function getPos(&$str,$currentpos,$nextlen){
$tmp = substr($str,$currentpos,$nextlen);
$rawtextarray = preg_split("//",$tmp,-1, PREG_SPLIT_NO_EMPTY);
$rawtext = array();
for($i=0;$i<count($rawtextarray);$i++)$rawtext[] = ord($rawtextarray[$i]); //ord(char)
$rawtextlen = $nextlen;
//<for***********>
for($i=0;$i<$rawtextlen;$i++){
//<if>
if ($rawtext[$i] < 0x80) { // One byte
$asciibytes++; // Ignore ASCII, can throw off count
}
else if (0xC0 <= $rawtext[$i] && $rawtext[$i] <= 0xDF && // Two bytes
$i+1 < $rawtextlen && 0x80 <= $rawtext[$i+1] && $rawtext[$i+1] <= 0xBF) {
$goodbytes += 2; $i++;
}
else if (0xE0 <= $rawtext[$i] && $rawtext[$i] <= 0xEF && // Three bytes
$i+2 < $rawtextlen && 0x80 <= $rawtext[$i+1] && $rawtext[$i+1] <= 0xBF &&
0x80 <= $rawtext[$i+2] && $rawtext[$i+2] <= 0xBF) {
$goodbytes += 3; $i+=2;
}//</if>
}//</for*********>
//echo "ascii字节数:$asciibytes \n";
if($asciibytes > 0){
$offset =3-($asciibytes%3);
$nextpos = $currentpos + $nextlen - $offset ;
}
else{
$nextpos = $currentpos + $nextlen;
}
return($nextpos);
}
?>