<?php /** * Description: * Some functions for string handling * * History: * Created by Moky @ 2009-07-28 */ function substring_gb2312($str, $len) { if ($len <= 0) return ''; if ($len >= strlen($str)) return $str; for ($i = 0; $i < $len; $i++) if (ord($str[$i]) > 128) $i++; return substr($str, 0, $i); } function substring_utf8($str, $len) { if ($len <= 0) return ''; if ($len >= strlen($str)) return $str; for ($i = 0; $i < $len; $i++) { $c = ord($str[$i]); if ($c > 0xC0) $i += 2; // 1100 0000 else if ($c > 0x80) $i += 1; // 1000 0000 } return substr($str, 0, $i); } function substring($str, $len) { return substring_utf8($str, $len); } function cut_string($str, $len) { $s = substring($str, $len); if ($s != $str) $s .= '...'; return $s; } function plain_cut_string($str, $len) { $s = strip_tags($str); $s = str_replace(' ', '', $s); $s = str_replace("/r", '', $s); $s = str_replace("/n", ' ', $s); return cut_string($s, $len); } ?>