note 53042 deleted from function.wordwrap by nlopess

From: Date: Sat, 24 Nov 2007 11:30:40 +0000
Subject: note 53042 deleted from function.wordwrap by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-132532@lists.php.net to get a copy of this message
Note Submitter: cdblog at gmail dot com ---- note: if you can't see the example, please change your browser's default encode to Chinese Simplified (GB2312). :) also, this function maybe will work at other double-bytes language, such as Korean. <?php /** * @return string * @author Cocol * @desc the wordwrap for Chinese ,Janpanese * for more detail please visit http://php.clickz.cn/articles/string/wrodwrap.html */ function wordwrap_cn($str = "",$len = 80,$glue = "\n") { for ($i=0;$i<strlen($str);$i++) { if (ord(substr($str,$i,1)) > 128) { $str_arr[] = substr($str,$i,2); $i++; } else { $str_arr[] = substr($str,$i,1); } } $tmp = array_chunk($str_arr,$len); $str = ""; foreach ($tmp as $key=>$val) { $str .= implode("",$val).$glue; } return $str; } //example 1: Chinese test $str = "ËüÊÕ¼ÁËÈ«Çò×ÊÑ¶ÍøÉÏÊýÒÔÍò¼ÆµÄÖÐÎÄÍøÕ¾£¬²»ÂÛ"; print wordwrap_cn($str,10); // will output /* ËüÊÕ¼ÁËÈ«Çò×ÊÑ¶ÍøÉÏ ÊýÒÔÍò¼ÆµÄÖÐÎÄÍøÕ¾£¬ ²»ÂÛ //example 2: Janpanese test */ $str = "±¦‰VӛÄî¤Ë³ö×ߤµ¤»¤¿¤¤ñR¤ËͶƱ¤ò"; print wordwrap_cn($str,10); // will output /* ±¦‰VӛÄî¤Ë³ö×ߤµ¤»¤¿ ¤¤ñR¤ËͶƱ¤ò */ ?>

« previous php.notes (#132532) next »