I am writing a script to let me know the database login info for each PHP-based web site on a server that I am migrating. The login information is usually done either like this:
$servername='localhost';
$database_username='someName';
$database_password='p@ssw0rd';
$database_name='someDatabase';
mysql_connect($servername, $database_username, $database_password);
mysql_select_db($database_name);
Or like this:
$pdo = new PDO("mysql: host={$servername}; dbname={$$database_name}", $database_username, $database_password);
The script is searching for each line which contains one of the PHP connection functions:
grep -irE "mysql_connect\|mysql_select_db\|new PDO" *
Now, how can I get each string that begins with $
and ends with a non-alphanumeric? Additionally, if no ;
character is found on the screen I would like to be notified so that I could handle it manually, as some coders might do this:
$pdo = new PDO("mysql: host={$servername};
dbname={$$database_name}",
$database_username,
$database_password
);
After I get these tokens (PHP variables), I'll grep the file again for the tokens in hope to get their values;