fork download
  1. <?php
  2. $sql = 'SELECT
  3. [Person].[Name]
  4. [Person].[Address],
  5. [Sales].[TotalAmount]';
  6.  
  7. preg_match_all('#\[[^]]+\]\.(\[[^]]+\])#', $sql, $found); // Match
  8. $max = max(array_map('strlen', $found[1])); // Find max length
  9.  
  10. $new = preg_replace_callback('#\[[^]]+\]\.(\[[^]]+\])#', function($m)use($max){
  11. $string = $m[1].str_repeat(' ',$max - strlen($m[1])).' = '.$m[0];
  12. return($string);
  13. }, $sql);
  14. echo $new;
  15. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
SELECT
    [Name]        = [Person].[Name]
    [Address]     = [Person].[Address],
    [TotalAmount] = [Sales].[TotalAmount]