Transforming a string to a valid PDO_MYSQL DSN

Posted by Alix Axel on Stack Overflow See other posts from Stack Overflow or by Alix Axel
Published on 2010-04-28T06:40:47Z Indexed on 2010/04/28 6:43 UTC
Read the original article Hit count: 351

Filed under:
|
|
|
|

What is the most concise way to transform a string in the following format:

mysql:[/[/]][user[:pass]@]host[:port]/db[/]

Into a usuable PDO connection/instance (using the PDO_MYSQL DSN), some possible examples:

$conn = new PDO('mysql:host=host;dbname=db');
$conn = new PDO('mysql:host=host;port=3307;dbname=db');
$conn = new PDO('mysql:host=host;port=3307;dbname=db', 'user');
$conn = new PDO('mysql:host=host;port=3307;dbname=db', 'user', 'pass');

I've been trying some regular expressions (preg_[match|split|replace]) but they either don't work or are too complex, my gut tells me this is not the way to go but nothing else comes to my mind.

Any suggestions?

© Stack Overflow or respective owner

Related posts about php

Related posts about pdo