Re: regex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Lucas wrote:
Peter wrote:
I am trying to convert ms access sql to postgresql using php.

I have a sql statement in the form ;-
$sql = SELECT DISTINCT [Table Name].[Column.Name], [Table Name 1].[Column Name 2] etc.

what I want to end up with is $sql = SELECT DISTINCT table_name.column_name, table_name_1.column_name_2, ........



Something along the lines of this?

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$sql = "SELECT DISTINCT [Table Name].[Column Name], ".
    "[Table Name 1].[Column Name 2]";

echo "{$sql}<br />\n";

preg_match_all('%\[([^\]]+)\]%', $sql, $matches, PREG_SET_ORDER);

$change = array();
foreach ( $matches AS $match ) {
    $change[$match[0]] = str_replace(' ', '_', $match[1]);
}

echo '<pre>'.print_r($change,1).'</pre>';

echo str_replace(array_keys($change), $change, $sql);


Jim

 You are a genius! Worked like a charm.
I ran the string thru strlower and strreplace first then thru your code and it ended up exactly as I required it (all table/columns lower case, no % $ in column names etc).

 Thank you very much.

 Peter

(Keith I havent tried yours as yet)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux