Last active
February 26, 2018 01:38
-
-
Save nhalstead/c66db15c4123df4724a718f31e9200ec to your computer and use it in GitHub Desktop.
DB String Config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* DB String Processing | |
* Provide something like `mysql://admin:[email protected]:4844/mydbselection` | |
* and get back an array of the data, sorted to the proper data. | |
* | |
* @link https://regex101.com/r/e8gkkx/2/ | |
* @param String Database URL Config Processing | |
* @return Array Reutnrs and Array with Type, Username, Password, Host, Port, HostPort, DB | |
*/ | |
function dbProcess($url){ | |
$re = '/([a-z]*):\/\/([a-zA-Z0-9.]*):([a-zA-Z0-9.]*)@([a-zA-Z0-9.]*)(:([0-9]*)|)(\/[a-zA-Z0-9]*|)/ix'; | |
preg_match_all($re, $url, $matches, PREG_SET_ORDER, 0); | |
// Match 1 | |
// Full match 0-51 `mysql://admin:[email protected]:4844/mydbselection` | |
// Group 1. 0-5 `mysql` | |
// Group 2. 8-13 `admin` | |
// Group 3. 14-22 `password` | |
// Group 4. 23-32 `127.0.0.1` | |
// Group 5. 32-37 `:4844` | |
// Group 6. 33-37 `4844` | |
// Group 7. 38-51 `mydbselection` | |
$matches = $matches[0]; | |
$r['type'] = $matches[1]; | |
$r['username'] = $matches[2]; | |
$r['password'] = $matches[3]; | |
$r['host'] = $matches[4]; | |
$r['port'] = preg_replace("/[^0-9]/", "", t($matches[6], 3307) ); // Make Int | |
$r['hostport'] = $r['host'].":".$r['port']; | |
$r['db'] = preg_replace('/^\//', '', $matches[7] ); | |
return $r; | |
} | |
/** | |
* Check the input to see if it is Null, Not Set, or Blank | |
* @param Mixed Object to test | |
* @param Mixed Optinal, Default Output on empty | |
*/ | |
function t(&$v, $d = ""){ | |
if( strlen($v) == 0 || $v == "" || $v == null || preg_match('/^\s*$/', $v) ){ | |
return $d; | |
} | |
return $v; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment