Transforming a string to a valid PDO_MYSQL DSN
- by Alix Axel
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?