/**
- * Chinese string intercept function
- * by bbs.it-home.org
- */
- function Cut_str ($string, $sublen, $filter =true, $start =0, $code = ' UTF-8 ') {
- if ($filter) $string =html2text ($string);
- if ($code = = ' UTF-8 ') {
- $pa = "/[\x01-\x7f]| [\XC2-\XDF] [\x80-\xbf]| [\xe0-\xef] [\X80-\XBF] {2}| [\xf0-\xff] [\X80-\XBF] {3}/";
- Preg_match_all ($pa, $string, $t _string);
- if (count ($t _string[0])-$start > $sublen) return join ("', Array_slice ($t _string[0], $start, $sublen))." ...";
- return join ("', Array_slice ($t _string[0], $start, $sublen));
- }else{
- $start = $start;
- $sublen = $sublen;
- $strlen =strlen ($string);
- $tmpstr = ";
- for ($i =0; $i < $strlen; $i + +) {
- if ($i >= $start && $i < ($start + $sublen)) {
- if (Ord (substr ($string, $i, 1)) >129) {
- $tmpstr. =substr ($string, $i, 2);
- }else{
- $tmpstr. =substr ($string, $i, 1);
- }
- }
- if (Ord (substr ($string, $i, 1)) >129) $i + +;
- }
- if (strlen ($TMPSTR) < $strlen) $tmpstr. = "...";
- return $tmpstr;
- }
- }
HTML Conversion Functions
- function Html2text ($STR) {
- $str = Preg_replace ("/ | | /isu "," ", $str);
- $alltext = "";
- $start = 1;
- for ($i =0; $i<>
- if ($start ==0 && $str [$i]== ">") {
- $start = 1;
- }else if ($start ==1) {
- if ($str [$i]== "<") {
- $start = 0;
- $alltext. = "";
- }else if (Ord ($str [$i]) >31) {
- $alltext. = $str [$i];
- }
- }
- }
- $alltext = Str_replace ("", "" ", $alltext);
- $alltext = Preg_replace ("/& ([^;&]*) (;|&)/", "", $alltext);
- $alltext = Preg_replace ("/[]+/s", "", $alltext);
- return $alltext;
- }
Copy Code |