string_class.php
2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* String 类
*
* @author Tiny
* @class TString
*/
class TString
{
public static function msubstr($str, $start = 0, $length, $charset = "utf-8", $ext = '...')
{
if (function_exists("mb_substr")) {
$slice = mb_substr($str, $start, $length, $charset);
} elseif (function_exists('iconv_substr')) {
$slice = iconv_substr($str, $start, $length, $charset);
} else {
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[0], $start, $length));
}
return strlen($str) > strlen($slice) ? $slice . $ext : $slice;
}
public static function parseName($name, $type = 0)
{
if ($type) {
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
} else {
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
}
public static function strlen($str)
{
if (empty($str)) {
return 0;
}
if (function_exists('mb_strlen')) {
return mb_strlen($str, 'utf-8');
} else {
preg_match_all("/./u", $str, $ar);
return count($ar[0]);
}
}
public static function nl2br($str)
{
return str_replace(array("\r\n", "\r", "\n"), "<br/>", $str);
}
public static function startWith($str, $needle)
{
return strpos($str, $needle) === 0;
}
public static function endWith($str, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
}