1: <?php
2:
3: namespace ngatngay;
4:
5: class str
6: {
7: /**
8: * @param string $string
9: * @return bool
10: */
11: public static function empty($string)
12: {
13: return strlen($string) === 0;
14: }
15:
16: /**
17: * @param string $string
18: * @param int $words
19: * @param string $end
20: * @return string
21: */
22: public static function word_cut($string, $words = 35, $end = '...')
23: {
24: preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $string, $matches);
25:
26: if (!isset($matches[0]) || self::length($string) === self::length($matches[0])) {
27: return $string;
28: }
29:
30: return rtrim($matches[0]) . $end;
31: }
32:
33: /**
34: * @param string $str
35: * @return string
36: */
37: public static function br2nl($str)
38: {
39: return preg_replace('#<br\s*/?>#i', PHP_EOL, $str);
40: }
41:
42: /**
43: * @param string $str
44: * @return int
45: */
46: public static function length($str)
47: {
48: return mb_strlen($str);
49: }
50:
51: /**
52: * Chuyển đổi tiếng Việt sang tiếng Anh
53: * @param string $str
54: * @return string
55: */
56: public static function vn2en($str)
57: {
58: $unicode = [
59: 'a' => '/á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ/',
60: 'd' => '/đ/',
61: 'e' => '/é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ/',
62: 'i' => '/í|ì|ỉ|ĩ|ị/',
63: 'o' => '/ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ/',
64: 'u' => '/ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự/',
65: 'y' => '/ý|ỳ|ỷ|ỹ|ỵ/',
66: 'A' => '/Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ằ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ/',
67: 'D' => '/Đ/',
68: 'E' => '/É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ/',
69: 'I' => '/Í|Ì|Ỉ|Ĩ|Ị/',
70: 'O' => '/Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ/',
71: 'U' => '/Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự/',
72: 'Y' => '/Ý|Ỳ|Ỷ|Ỹ|Ỵ/'
73: ];
74:
75: return preg_replace(array_values($unicode), array_keys($unicode), $str);
76: }
77:
78: /**
79: * @param string $needle
80: * @param string $replace
81: * @param string $haystack
82: * @return string
83: */
84: public static function replace_first($needle, $replace, $haystack)
85: {
86: $pos = strpos($haystack, $needle);
87:
88: if ($pos !== false) {
89: return substr_replace($haystack, $replace, $pos, strlen($needle));
90: }
91:
92: return $haystack;
93: }
94:
95: /**
96: * @param string $content
97: * @return string
98: */
99: public static function to_unix($content) {
100: return str_replace("\r\n", "\n", $content);
101: }
102: }
103: