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