[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 class text 24 { 25 /** 26 @function isEmail 27 28 Checks if "email" var is a valid email address. 29 30 Changed code, from http://www.iamcal.com/publish/articles/php/parsing_email/ 31 32 @param email string Email string 33 @return boolean 34 */ 35 public static function isEmail($email) 36 { 37 $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'; 38 $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'; 39 $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'; 40 $quoted_pair = '\\x5c[\\x00-\\x7f]'; 41 $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d"; 42 $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22"; 43 $domain_ref = $atom; 44 $sub_domain = "($domain_ref|$domain_literal)"; 45 $word = "($atom|$quoted_string)"; 46 $domain = "$sub_domain(\\x2e$sub_domain)*"; 47 $local_part = "$word(\\x2e$word)*"; 48 $addr_spec = "$local_part\\x40$domain"; 49 50 return (boolean) preg_match("!^$addr_spec$!", $email); 51 } 52 53 /** 54 @function str2URL 55 56 Transforms a string to a proper URL. 57 58 @param str string String to transform 59 @param with_slashes boolean Keep slashes in URL 60 @return string 61 */ 62 public static function str2URL($str,$with_slashes=true) 63 { 64 $pattern['A'] = '\x{00C0}-\x{00C5}'; 65 $pattern['AE'] = '\x{00C6}'; 66 $pattern['C'] = '\x{00C7}'; 67 $pattern['D'] = '\x{00D0}'; 68 $pattern['E'] = '\x{00C8}-\x{00CB}'; 69 $pattern['I'] = '\x{00CC}-\x{00CF}'; 70 $pattern['N'] = '\x{00D1}'; 71 $pattern['O'] = '\x{00D2}-\x{00D6}\x{00D8}'; 72 $pattern['OE'] = '\x{0152}'; 73 $pattern['S'] = '\x{0160}'; 74 $pattern['U'] = '\x{00D9}-\x{00DC}'; 75 $pattern['Y'] = '\x{00DD}'; 76 $pattern['Z'] = '\x{017D}'; 77 78 $pattern['a'] = '\x{00E0}-\x{00E5}'; 79 $pattern['ae'] = '\x{00E6}'; 80 $pattern['c'] = '\x{00E7}'; 81 $pattern['d'] = '\x{00F0}'; 82 $pattern['e'] = '\x{00E8}-\x{00EB}'; 83 $pattern['i'] = '\x{00EC}-\x{00EF}'; 84 $pattern['n'] = '\x{00F1}'; 85 $pattern['o'] = '\x{00F2}-\x{00F6}\x{00F8}'; 86 $pattern['oe'] = '\x{0153}'; 87 $pattern['s'] = '\x{0161}'; 88 $pattern['u'] = '\x{00F9}-\x{00FC}'; 89 $pattern['y'] = '\x{00FD}\x{00FF}'; 90 $pattern['z'] = '\x{017E}'; 91 92 $pattern['ss'] = '\x{00DF}'; 93 94 foreach ($pattern as $r => $p) { 95 $str = preg_replace('/['.$p.']/u',$r,$str); 96 } 97 98 $str = preg_replace('/[^A-Za-z0-9_\s\'\:\/[\]-]/','',$str); 99 100 return self::tidyURL($str,$with_slashes); 101 } 102 103 /** 104 @function tidyURL 105 106 Cleans an URL. 107 108 @param str string URL to tidy 109 @param keep_slashes boolean Keep slashes in URL 110 @param keep_spaces boolean Keep spaces in URL 111 @return string 112 */ 113 public static function tidyURL($str,$keep_slashes=true,$keep_spaces=false) 114 { 115 $str = strip_tags($str); 116 $str = str_replace(array('?','&','#','=','+','<','>'),'',$str); 117 $str = str_replace("'",'',$str); 118 $str = preg_replace('/[\s]+/',' ',trim($str)); 119 120 if (!$keep_slashes) { 121 $str = str_replace('/','-',$str); 122 } 123 124 if (!$keep_spaces) { 125 $str = str_replace(' ','-',$str); 126 } 127 128 $str = preg_replace('/[-]+/','-',$str); 129 130 # Remove path changes in URL 131 $str = preg_replace('%^/%','',$str); 132 $str = preg_replace('%\.+/%','',$str); 133 134 return $str; 135 } 136 137 /** 138 @function cutString 139 140 Cuts a string on spaces. 141 142 @param string str String to cut 143 @param integer l Lenght to keep 144 @return string 145 */ 146 public static function cutString($str,$l) 147 { 148 $s = preg_split('/([\s]+)/u',$str,-1,PREG_SPLIT_DELIM_CAPTURE); 149 150 $res = ''; 151 $L = 0; 152 153 if (strlen($s[0]) >= $l) { 154 return substr($s[0],0,$l); 155 } 156 157 foreach ($s as $v) 158 { 159 $L = $L+strlen($v); 160 161 if ($L > $l) { 162 break; 163 } else { 164 $res .= $v; 165 } 166 } 167 168 return trim($res); 169 } 170 171 /** 172 @function splitWords 173 174 Returns an array of words from a given string. 175 176 @param str string Words to split 177 @return array 178 */ 179 public static function splitWords($str) 180 { 181 $non_word = '\x{0000}-\x{002F}\x{003A}-\x{0040}\x{005b}-\x{0060}\x{007B}-\x{007E}\x{00A0}-\x{00BF}\s'; 182 if (preg_match_all('/([^'.$non_word.']{3,})/msu',html::clean($str),$match)) { 183 foreach ($match[1] as $i => $v) { 184 $match[1][$i] = mb_strtolower($v); 185 } 186 return $match[1]; 187 } 188 return array(); 189 } 190 191 /** 192 @function detectEncoding 193 194 Returns the encoding (in lowercase) of given $str. 195 196 @param str string String 197 @return string 198 */ 199 public static function detectEncoding($str) 200 { 201 return strtolower(mb_detect_encoding($str.' ', 202 'UTF-8,ISO-8859-1,ISO-8859-2,ISO-8859-3,'. 203 'ISO-8859-4,ISO-8859-5,ISO-8859-6,ISO-8859-7,ISO-8859-8,'. 204 'ISO-8859-9,ISO-8859-10,ISO-8859-13,ISO-8859-14,ISO-8859-15')); 205 } 206 207 /** 208 @function toUTF8 209 210 Return an UTF-8 converted string. 211 212 @param str string String to convert 213 @param encoding string Optionnal "from" encoding 214 @return string 215 */ 216 public static function toUTF8($str,$encoding=null) 217 { 218 if (!$encoding) { 219 $encoding = self::detectEncoding($str); 220 } 221 222 if ($encoding != 'utf-8') { 223 $str = iconv($encoding,'UTF-8',$str); 224 } 225 226 return $str; 227 } 228 229 /** 230 @function utf8badFind 231 232 Taken from http://phputf8.sourceforge.net 233 234 Locates the first bad byte in a UTF-8 string returning it's 235 byte index in the string 236 PCRE Pattern to locate bad bytes in a UTF-8 string 237 Comes from W3 FAQ: Multilingual Forms 238 Note: modified to include full ASCII range including control chars 239 240 @param str string String to search 241 @return mixed 242 */ 243 public static function utf8badFind($str) 244 { 245 $UTF8_BAD = 246 '([\x00-\x7F]'. # ASCII (including control chars) 247 '|[\xC2-\xDF][\x80-\xBF]'. # non-overlong 2-byte 248 '|\xE0[\xA0-\xBF][\x80-\xBF]'. # excluding overlongs 249 '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'. # straight 3-byte 250 '|\xED[\x80-\x9F][\x80-\xBF]'. # excluding surrogates 251 '|\xF0[\x90-\xBF][\x80-\xBF]{2}'. # planes 1-3 252 '|[\xF1-\xF3][\x80-\xBF]{3}'. # planes 4-15 253 '|\xF4[\x80-\x8F][\x80-\xBF]{2}'. # plane 16 254 '|(.{1}))'; # invalid byte 255 $pos = 0; 256 $badList = array(); 257 258 while (preg_match('/'.$UTF8_BAD.'/S', $str, $matches)) { 259 $bytes = strlen($matches[0]); 260 if ( isset($matches[2])) { 261 return $pos; 262 } 263 $pos += $bytes; 264 $str = substr($str,$bytes); 265 } 266 return false; 267 } 268 269 /** 270 @function cleanUTF8 271 272 Taken from http://phputf8.sourceforge.net/ 273 274 Replace non utf8 bytes in $str by $repl. 275 276 @param str string String to clean 277 @param repl string Replacement string 278 @return string 279 */ 280 public static function cleanUTF8($str,$repl='?') 281 { 282 while (($bad_index = self::utf8badFind($str)) !== false) { 283 $str = substr_replace($str,$repl,$bad_index,1); 284 } 285 286 return $str; 287 } 288 289 /** 290 @function removeBOM 291 292 Removes BOM from the begining of a string if present. 293 294 @param str string String to clean 295 @return string 296 */ 297 public static function removeBOM($str) 298 { 299 if (substr_count($str,'')) { 300 return str_replace('','',$str); 301 } 302 303 return $str; 304 } 305 306 /** 307 @function QPEncode 308 309 Encodes given str to quoted printable 310 311 @param str string String to encode 312 @return string 313 */ 314 public static function QPEncode($str) 315 { 316 $res = ''; 317 318 foreach (preg_split("/\r?\n/msu", $str) as $line) 319 { 320 $l = ''; 321 preg_match_all('/./',$line,$m); 322 323 foreach ($m[0] as $c) 324 { 325 $a = ord($c); 326 327 if ($a < 32 || $a == 61 || $a > 126) { 328 $c = sprintf('=%02X',$a); 329 } 330 331 $l .= $c; 332 } 333 334 $res .= $l."\r\n"; 335 } 336 return $res; 337 } 338 } 339 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |