Db::parseDSN PHP Méthode

parseDSN() public méthode

+---------------------------------------------------------- DSN解析 格式: mysql://username:passwd@localhost:3306/DbName
public parseDSN ( string $dsnStr ) : array
$dsnStr string
Résultat array
    public function parseDSN($dsnStr)
    {
        if (empty($dsnStr)) {
            return false;
        }
        $info = parse_url($dsnStr);
        if ($info['scheme']) {
            $dsn = array('dbms' => $info['scheme'], 'username' => isset($info['user']) ? $info['user'] : '', 'password' => isset($info['pass']) ? $info['pass'] : '', 'hostname' => isset($info['host']) ? $info['host'] : '', 'hostport' => isset($info['port']) ? $info['port'] : '', 'database' => isset($info['path']) ? substr($info['path'], 1) : '');
        } else {
            preg_match('/^(.*?)\\:\\/\\/(.*?)\\:(.*?)\\@(.*?)\\:([0-9]{1, 6})\\/(.*?)$/', trim($dsnStr), $matches);
            $dsn = array('dbms' => $matches[1], 'username' => $matches[2], 'password' => $matches[3], 'hostname' => $matches[4], 'hostport' => $matches[5], 'database' => $matches[6]);
        }
        return $dsn;
    }

Usage Example

Exemple #1
0
 /**
  * Validate user email.
  *
  * @param string $value
  */
 public function validate($value = NULL, $type = 0)
 {
     if ($value or $type) {
         if (!Db::parseDSN($value)) {
             $this->element->error(t('Обратите внимание на ошибки и устраните их.'));
             return FALSE;
         }
         return TRUE;
     }
     return TRUE;
 }