[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /** 4 * I18N Utility file. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the BSD License. 8 * 9 * Copyright(c) 2004 by Wei Zhuo. All rights reserved. 10 * 11 * To contact the author write to <weizhuo[at]gmail[dot]com> 12 * The latest version of PRADO can be obtained from: 13 * {@link http://prado.sourceforge.net/} 14 * 15 * @author Wei Zhuo <weizhuo[at]gmail[dot]com> 16 * @version $Id: util.php 2757 2006-11-18 10:11:00Z fabien $ 17 * @package symfony 18 * @subpackage i18n 19 */ 20 21 22 /** 23 * For a given DSN (database connection string), return some information 24 * about the DSN. This function comes from PEAR's DB package. 25 * @param string DSN format, similar to PEAR's DB 26 * @return array DSN information. 27 */ 28 function parseDSN($dsn) 29 { 30 if (is_array($dsn)) { 31 return $dsn; 32 } 33 34 $parsed = array( 35 'phptype' => false, 36 'dbsyntax' => false, 37 'username' => false, 38 'password' => false, 39 'protocol' => false, 40 'hostspec' => false, 41 'port' => false, 42 'socket' => false, 43 'database' => false 44 ); 45 46 // Find phptype and dbsyntax 47 if (($pos = strpos($dsn, '://')) !== false) { 48 $str = substr($dsn, 0, $pos); 49 $dsn = substr($dsn, $pos + 3); 50 } else { 51 $str = $dsn; 52 $dsn = NULL; 53 } 54 55 // Get phptype and dbsyntax 56 // $str => phptype(dbsyntax) 57 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) { 58 $parsed['phptype'] = $arr[1]; 59 $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2]; 60 } else { 61 $parsed['phptype'] = $str; 62 $parsed['dbsyntax'] = $str; 63 } 64 65 if (empty($dsn)) { 66 return $parsed; 67 } 68 69 // Get (if found): username and password 70 // $dsn => username:password@protocol+hostspec/database 71 if (($at = strrpos($dsn,'@')) !== false) { 72 $str = substr($dsn, 0, $at); 73 $dsn = substr($dsn, $at + 1); 74 if (($pos = strpos($str, ':')) !== false) { 75 $parsed['username'] = rawurldecode(substr($str, 0, $pos)); 76 $parsed['password'] = rawurldecode(substr($str, $pos + 1)); 77 } else { 78 $parsed['username'] = rawurldecode($str); 79 } 80 } 81 82 // Find protocol and hostspec 83 84 // $dsn => proto(proto_opts)/database 85 if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) { 86 $proto = $match[1]; 87 $proto_opts = (!empty($match[2])) ? $match[2] : false; 88 $dsn = $match[3]; 89 90 // $dsn => protocol+hostspec/database (old format) 91 } else { 92 if (strpos($dsn, '+') !== false) { 93 list($proto, $dsn) = explode('+', $dsn, 2); 94 } 95 if (strpos($dsn, '/') !== false) { 96 list($proto_opts, $dsn) = explode('/', $dsn, 2); 97 } else { 98 $proto_opts = $dsn; 99 $dsn = null; 100 } 101 } 102 103 // process the different protocol options 104 $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp'; 105 $proto_opts = rawurldecode($proto_opts); 106 if ($parsed['protocol'] == 'tcp') { 107 if (strpos($proto_opts, ':') !== false) { 108 list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts); 109 } else { 110 $parsed['hostspec'] = $proto_opts; 111 } 112 } elseif ($parsed['protocol'] == 'unix') { 113 $parsed['socket'] = $proto_opts; 114 } 115 116 // Get dabase if any 117 // $dsn => database 118 if (!empty($dsn)) { 119 // /database 120 if (($pos = strpos($dsn, '?')) === false) { 121 $parsed['database'] = $dsn; 122 // /database?param1=value1¶m2=value2 123 } else { 124 $parsed['database'] = substr($dsn, 0, $pos); 125 $dsn = substr($dsn, $pos + 1); 126 if (strpos($dsn, '&') !== false) { 127 $opts = explode('&', $dsn); 128 } else { // database?param1=value1 129 $opts = array($dsn); 130 } 131 foreach ($opts as $opt) { 132 list($key, $value) = explode('=', $opt); 133 if (!isset($parsed[$key])) { // don't allow params overwrite 134 $parsed[$key] = rawurldecode($value); 135 } 136 } 137 } 138 } 139 140 return $parsed; 141 } 142 143 /** 144 * Convert strings to UTF-8 via iconv. NB, the result may not by UTF-8 145 * if the conversion failed. 146 * @param string string to convert to UTF-8 147 * @return string UTF-8 encoded string, original string if iconv failed. 148 */ 149 function I18N_toUTF8($string, $from) 150 { 151 $from = strtoupper($from); 152 if ($from != 'UTF-8') 153 { 154 $s = iconv($from,'UTF-8',$string); //to UTF-8 155 return $s !== false ? $s : $string; //it could return false 156 } 157 158 return $string; 159 } 160 161 /** 162 * Convert UTF-8 strings to a different encoding. NB. The result 163 * may not have been encoded if iconv fails. 164 * @param string the UTF-8 string for conversion 165 * @return string encoded string. 166 */ 167 function I18N_toEncoding($string, $to) 168 { 169 $to = strtoupper($to); 170 if ($to != 'UTF-8') 171 { 172 $s = iconv('UTF-8', $to, $string); 173 return $s !== false ? $s : $string; 174 } 175 176 return $string; 177 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |