function parseDSN($dsn)
	{
		if (is_array($dsn)) {
			return $dsn;
		}

		$parsed = array(
			'phptype'  => false,
			'dbsyntax' => false,
			'protocol' => false,
			'hostspec' => false,
			'database' => false,
			'username' => false,
			'password' => false
		);

		// Find phptype and dbsyntax
		if (($pos = strpos($dsn, '://')) !== false) {
			$str = substr($dsn, 0, $pos);
			$dsn = substr($dsn, $pos + 3);
		} else {
			$str = $dsn;
			$dsn = NULL;
		}

		// Get phptype and dbsyntax
		// $str => phptype(dbsyntax)
		if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
			$parsed['phptype'] = $arr[1];
			$parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
		} else {
			$parsed['phptype'] = $str;
			$parsed['dbsyntax'] = $str;
		}

		if (empty($dsn)) {
			return $parsed;
		}

		// Get (if found): username and password
		// $dsn => username:password@protocol+hostspec/database
		if (($at = strpos($dsn,'@')) !== false) {
			$str = substr($dsn, 0, $at);
			$dsn = substr($dsn, $at + 1);
			if (($pos = strpos($str, ':')) !== false) {
				$parsed['username'] = urldecode(substr($str, 0, $pos));
				$parsed['password'] = urldecode(substr($str, $pos + 1));
			} else {
				$parsed['username'] = urldecode($str);
			}
		}

		// Find protocol and hostspec
		// $dsn => protocol+hostspec/database
		if (($pos=strpos($dsn, '/')) !== false) {
			$str = substr($dsn, 0, $pos);
			$dsn = substr($dsn, $pos + 1);
		} else {
			$str = $dsn;
			$dsn = NULL;
		}

		// Get protocol + hostspec
		// $str => protocol+hostspec
		if (($pos=strpos($str, '+')) !== false) {
			$parsed['protocol'] = substr($str, 0, $pos);
			$parsed['hostspec'] = urldecode(substr($str, $pos + 1));
		} else {
			$parsed['hostspec'] = urldecode($str);
		}

		// Get dabase if any
		// $dsn => database
		if (!empty($dsn)) {
			$parsed['database'] = $dsn;
		}

		return $parsed;
	}
function parseDSN($dsn)
	{
		if (is_array($dsn)) {
			return $dsn;
		}

		$parsed = array(
			'phptype'  => false,
			'dbsyntax' => false,
			'protocol' => false,
			'hostspec' => false,
			'database' => false,
			'username' => false,
			'password' => false
		);

		// Find phptype and dbsyntax
		if (($pos = strpos($dsn, '://')) !== false) {
			$str = substr($dsn, 0, $pos);
			$dsn = substr($dsn, $pos + 3);
		} else {
			$str = $dsn;
			$dsn = NULL;
		}

		// Get phptype and dbsyntax
		// $str => phptype(dbsyntax)
		if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
			$parsed['phptype'] = $arr[1];
			$parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
		} else {
			$parsed['phptype'] = $str;
			$parsed['dbsyntax'] = $str;
		}

		if (empty($dsn)) {
			return $parsed;
		}

		// Get (if found): username and password
		// $dsn => username:password@protocol+hostspec/database
		if (($at = strpos($dsn,'@')) !== false) {
			$str = substr($dsn, 0, $at);
			$dsn = substr($dsn, $at + 1);
			if (($pos = strpos($str, ':')) !== false) {
				$parsed['username'] = urldecode(substr($str, 0, $pos));
				$parsed['password'] = urldecode(substr($str, $pos + 1));
			} else {
				$parsed['username'] = urldecode($str);
			}
		}

		// Find protocol and hostspec
		// $dsn => protocol+hostspec/database
		if (($pos=strpos($dsn, '/')) !== false) {
			$str = substr($dsn, 0, $pos);
			$dsn = substr($dsn, $pos + 1);
		} else {
			$str = $dsn;
			$dsn = NULL;
		}

		// Get protocol + hostspec
		// $str => protocol+hostspec
		if (($pos=strpos($str, '+')) !== false) {
			$parsed['protocol'] = substr($str, 0, $pos);
			$parsed['hostspec'] = urldecode(substr($str, $pos + 1));
		} else {
			$parsed['hostspec'] = urldecode($str);
		}

		// Get dabase if any
		// $dsn => database
		if (!empty($dsn)) {
			$parsed['database'] = $dsn;
		}

		return $parsed;
	}
function parseDSN ( $ dsn ) { if ( is_array ( $ dsn ) ) { return $ dsn ; } $ parsed = array ( 'phptype' => false , 'dbsyntax' => false , 'protocol' => false , 'hostspec' => false , 'database' => false , 'username' => false , 'password' => false ) ; // Find phptype and dbsyntax if ( ( $ pos = strpos ( $ dsn , '://' ) ) !== false ) { $ str = substr ( $ dsn , 0 , $ pos ) ; $ dsn = substr ( $ dsn , $ pos + 3 ) ; } else { $ str = $ dsn ; $ dsn = NULL ; } // Get phptype and dbsyntax // $str => phptype(dbsyntax) if ( preg_match ( '|^(.+?)\((.*?)\)$|' , $ str , $ arr ) ) { $ parsed [ 'phptype' ] = $ arr [ 1 ] ; $ parsed [ 'dbsyntax' ] = ( empty ( $ arr [ 2 ] ) ) ? $ arr [ 1 ] : $ arr [ 2 ] ; } else { $ parsed [ 'phptype' ] = $ str ; $ parsed [ 'dbsyntax' ] = $ str ; } if ( empty ( $ dsn ) ) { return $ parsed ; } // Get (if found): username and password // $dsn => username:password@protocol+hostspec/database if ( ( $ at = strpos ( $ dsn , '@' ) ) !== false ) { $ str = substr ( $ dsn , 0 , $ at ) ; $ dsn = substr ( $ dsn , $ at + 1 ) ; if ( ( $ pos = strpos ( $ str , ':' ) ) !== false ) { $ parsed [ 'username' ] = urldecode ( substr ( $ str , 0 , $ pos ) ) ; $ parsed [ 'password' ] = urldecode ( substr ( $ str , $ pos + 1 ) ) ; } else { $ parsed [ 'username' ] = urldecode ( $ str ) ; } } // Find protocol and hostspec // $dsn => protocol+hostspec/database if ( ( $ pos = strpos ( $ dsn , '/' ) ) !== false ) { $ str = substr ( $ dsn , 0 , $ pos ) ; $ dsn = substr ( $ dsn , $ pos + 1 ) ; } else { $ str = $ dsn ; $ dsn = NULL ; } // Get protocol + hostspec // $str => protocol+hostspec if ( ( $ pos = strpos ( $ str , '+' ) ) !== false ) { $ parsed [ 'protocol' ] = substr ( $ str , 0 , $ pos ) ; $ parsed [ 'hostspec' ] = urldecode ( substr ( $ str , $ pos + 1 ) ) ; } else { $ parsed [ 'hostspec' ] = urldecode ( $ str ) ; } // Get dabase if any // $dsn => database if ( ! empty ( $ dsn ) ) { $ parsed [ 'database' ] = $ dsn ; } return $ parsed ; }
Parse a data source name

@param $dsn string Data Source Name to be parsed

@return array an associative array with the following keys:

phptype: Database backend used in PHP (mysql, odbc etc.)
dbsyntax: Database used with regards to SQL syntax etc.
protocol: Communication protocol to use (tcp, unix etc.)
hostspec: Host specification (hostname[:port])
database: Database to use on the DBMS server
username: User name for login
password: Password for login

The format of the supplied DSN is in its fullest form:

phptype(dbsyntax)://username:password@protocol+hostspec/database

Most variations are allowed:

phptype://username:password@protocol+hostspec:110//usr/db_file.db
phptype://username:password@hostspec/database_name
phptype://username:password@hostspec
phptype://username@hostspec
phptype://hostspec/database
phptype://hostspec
phptype(dbsyntax)
phptype

@author Tomas V.V.Cox <cox@idecnet.com>
Parse a data source name