Search code examples
phpsqlpdopeardsn

Convert PEAR::DB-style DSN into PDO-DSN


I am looking for a way how to convert a PEAR:DB / MDB2-style DSN

phptype://username:password@hostspec/database

into format supported by PHP PDO (such as this mysql case)

$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'username';
$password = 'password';

I was looking for some existing implementation but couldn't find anything.


Solution

  • I've solved problem with this code, but it's not yet properly tested:

    preg_match('|([a-z]+)://([^:]*)(:(.*))?@([A-Za-z0-9\.-]*)(/([0-9a-zA-Z_/\.]*))|',
         $dsn,$matches);
    $dsn=array(
        $matches[1].':host='.$matches[5].';dbname='.$matches[7],
        $matches[2],
        $matches[4]
    );