1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Strings
13: {
14: 15: 16: 17: 18: 19:
20: public static function webalize($string)
21: {
22: $string = trim($string);
23: $string = self::to_ascii($string);
24: $string = strtolower($string);
25: preg_match_all('/[a-zA-Z0-9]+/', $string, $new_string);
26: return implode('-', $new_string[0]);
27: }
28:
29:
30: 31: 32: 33: 34: 35:
36: public static function to_ascii($string)
37: {
38: if (ICONV_IMPL === 'glibc') {
39: $table = array(
40: 'á' => 'a', 'ä' => 'a', 'č' => 'c', 'ď' => 'd', 'é' => 'e',
41: 'ě' => 'e', 'ë' => 'e', 'í' => 'i', 'ĺ' => 'l', 'ľ' => 'l',
42: 'ň' => 'n', 'ô' => 'o', 'ó' => 'o', 'ö' => 'o', 'ŕ' => 'r',
43: 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ú' => 'u', 'ů' => 'u',
44: 'ü' => 'u', 'ý' => 'y', 'ž' => 'z', 'Á' => 'A', 'Ä' => 'A',
45: 'Č' => 'C', 'Ď' => 'D', 'É' => 'E', 'Ě' => 'E', 'Ë' => 'E',
46: 'Í' => 'I', 'Ľ' => 'L', 'Ň' => 'N', 'Ó' => 'O', 'Ö' => 'O',
47: 'Ô' => 'O', 'Ř' => 'R', 'Ŕ' => 'R', 'Š' => 'S', 'Ť' => 'T',
48: 'Ú' => 'U', 'Ů' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Ž' => 'Z',
49: 'ś' => 's', 'Ś' => 'S', 'Ĺ' => 'L',
50: );
51:
52: $string = strtr($string, $table);
53:
54: } else {
55: $string = @iconv('UTF-8', 'ASCII//TRANSLIT', $string);
56: }
57:
58: return str_replace(array('`', '\'', '"', '^', '~'), '', $string);
59: }
60:
61:
62: public static function simple_format($string)
63: {
64: if (empty($string) === TRUE) {
65: return '';
66: }
67:
68: $string = self::typography($string);
69:
70:
71: $string = preg_replace('/(\s+)((?:(?:(?:[^@,"\[\]\x5c\x00-\x20\x7f-\xff\.]|\x5c(?=[@,"\[\]\x5c\x00-\x20\x7f-\xff]))(?:[^@,"\[\]\x5c\x00-\x20\x7f-\xff\.]|(?<=\x5c)[@,"\[\]\x5c\x00-\x20\x7f-\xff]|\x5c(?=[@,"\[\]\x5c\x00-\x20\x7f-\xff])|\.(?=[^\.])){1,62}(?:[^@,"\[\]\x5c\x00-\x20\x7f-\xff\.]|(?<=\x5c)[@,"\[\]\x5c\x00-\x20\x7f-\xff])|[^@,"\[\]\x5c\x00-\x20\x7f-\xff\.]{1,2})|"(?:[^"]|(?<=\x5c)"){1,62}")@(?:(?!.{64})(?:[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.?|[a-zA-Z0-9])+\.(?:xn--[a-zA-Z0-9]+|[a-zA-Z]{2,6})|\[(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])(?:\.(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])){3}\]))([\s\.]+)/', '$1<a href="mailto:$2">$2</a>$3', $string);
72:
73: $string = preg_replace('@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@', '<a href="$1">$1</a>', $string);
74:
75:
76: $string = str_replace(chr(13).chr(10), chr(10), $string);
77: $string = str_replace(chr(13), chr(10), $string);
78: $string = str_replace(chr(10).chr(10), '</p><p>', $string);
79: $string = str_replace(chr(10), '<br />', $string);
80: return '<p>' . $string . '</p>';
81: }
82:
83:
84: public static function format($string, $top = 2, $safe = FALSE)
85: {
86: if (empty($string) === TRUE) {
87: return '';
88: }
89:
90: $hash = md5($string);
91:
92: if (file_exists(CACHE_DIR . '/texy/' . $hash) === TRUE
93: AND DEBUG === FALSE) {
94:
95: return file_get_contents(CACHE_DIR . '/texy/' . $hash);
96: }
97:
98:
99:
100: $texy = new Texy();
101: $texy->setOutputMode(Texy::XHTML1_STRICT);
102: $texy->imageModule->leftClass = 'left';
103: $texy->imageModule->rightClass = 'right';
104: $texy->imageModule->root = 'files/images';
105: $texy->headingModule->top = $top;
106: $texy->allowed['longwords'] = FALSE;
107: $texy->obfuscateEmail = FALSE;
108: $texy->allowed['html/tag'] = TRUE;
109: $texy->allowed['phrase/sup'] = TRUE;
110: $texy->allowed['phrase/sub'] = TRUE;
111: $texy->htmlOutputModule->lineWrap = FALSE;
112: $texy->htmlOutputModule->indent = FALSE;
113: $texy->mergeLines = FALSE;
114:
115: $texy->addHandler('image', array('Photos', 'texy_photo'));
116:
117: if ($safe === TRUE) {
118: TexyConfigurator::safeMode($texy);
119:
120: $texy->urlSchemeFilters[Texy::FILTER_ANCHOR] = '#https?:|ftp:|mailto:#A';
121: $texy->urlSchemeFilters[Texy::FILTER_IMAGE] = '#https?:#A';
122:
123: $texy->allowed['heading/surrounded'] = FALSE;
124: $texy->allowed['heading/underlined'] = FALSE;
125: $texy->mergeLines = FALSE;
126: }
127:
128: $html = $texy->process($string);
129:
130: file_put_contents(CACHE_DIR . '/texy/' . $hash, $html);
131:
132: return $html;
133: }
134:
135:
136: public static function typography($string)
137: {
138: if (empty($string) === TRUE) {
139: return '';
140: }
141:
142:
143: $string = str_replace('...', '…', $string);
144:
145:
146: $string = preg_replace('|(")(.*?)(")|ms', '„$2“', $string);
147:
148:
149: $string = str_replace(array(' )', '( '), array(') ', ' ('), $string);
150:
151:
152: $string = preg_replace('#(\d+?)(\%|l|ml|dl|cl|cm|mm|m|km|kg|g)([\s\.]+)#', '$1 $2$3', $string);
153: $string = preg_replace('|([\ \d]+?)m2|', '$1 m²', $string);
154: $string = preg_replace('|([\ \d]+?)m3|', '$1 m³', $string);
155:
156:
157: preg_match_all('|(\s+)(\d{4,})([\s\.\,]+)|', $string, $matches);
158: while (empty($matches[2]) === FALSE) {
159: foreach ($matches[2] as $number) {
160: $formated_number = number_format($number, 0, ',', ' ');
161: $formated_number = str_replace(' ', ' ', $formated_number);
162: $string = str_replace($number, $formated_number, $string);
163: }
164:
165: preg_match_all('|(\s+)(\d{4,})([\s\.\,]+)|', $string, $matches);
166: }
167:
168:
169: $string = preg_replace('| (.{1,2}) |', ' $1 ', $string);
170:
171:
172: $string = str_replace(array(' .', ' ,'), array('. ', ', '), $string);
173:
174:
175: $string = preg_replace('| {2,}|', ' ', $string);
176:
177: return $string;
178: }
179:
180: public static function split_by_upper_chars($string)
181: {
182: return preg_split('/([A-Z]+[a-z]*)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
183: }
184:
185:
186: public static function random_string($length = 9, $strength = 0)
187: {
188: $vowels = 'aeiouy';
189: $consonants = 'bdghjmnpqrstvz';
190: if ($strength & 1) {
191: $consonants .= 'BDGHJLMNPQRSTVWXZ';
192: }
193:
194: if ($strength & 2) {
195: $vowels .= 'AEIOUY';
196: }
197:
198: if ($strength & 4) {
199: $consonants .= '123456789';
200: }
201:
202: if ($strength & 8) {
203: $consonants .= '@#$%';
204: }
205:
206: $password = '';
207: $alt = microtime() % 2;
208: for ($i = 0; $i < $length; $i++) {
209: if ($alt === 1) {
210: $password .= $consonants[(rand() % strlen($consonants))];
211: $alt = 0;
212:
213: } else {
214: $password .= $vowels[(rand() % strlen($vowels))];
215: $alt = 1;
216: }
217: }
218:
219: return $password;
220: }
221:
222:
223: public static function wordwrap($str, $width, $break) {
224: $formatted = '';
225: $position = -1;
226: $prev_position = 0;
227: $last_line = -1;
228:
229: while($position = mb_stripos($str, ' ', ++$position, 'utf-8')) {
230: if($position > $last_line + $width + 1) {
231: $formatted.= mb_substr($str, $last_line + 1, $prev_position - $last_line - 1, 'utf-8').$break;
232: $last_line = $prev_position;
233: }
234: $prev_position = $position;
235: }
236:
237: $formatted.= mb_substr($str, $last_line + 1, mb_strlen($str), 'utf-8');
238: return $formatted;
239: }
240: }