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