Re: regex
| From: | Peter Jackson | Date: | Mon, 21 Jan 2008 22:18:34 +0000 |
| Subject: | Re: regex | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-267791@lists.php.net to get a copy of this message | ||
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, ........ I have managed to get the caps to lower but I cant work out how to put the _ in place of spaces if the spaces are between [ ]. I either end up with S_E_L_E_C ..... or SELECT_DISTINCT_ etc... .
Something along the lines of this? <?php error_reporting(E_ALL); ini_set('display_errors', 1); $sql = "SELECT DISTINCT [Table Name].[Column Name], ".Thanks Keith and Jim once I wake up I'll give both methods a try and let you know how I go. (Just having my morning coffee atm) I know I wont find the perfect converter as ms access lets you use to many 'bad' characters in column names. aah well only need to change about 600 queries. Peter"[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);