1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <?php
function explode_parser( $str, $delimiter=',')
{
$len = strlen($str);
for ($i = 0,$in_delimiter = true,$quote_is_begin = false,$tab = array(),$tmp = ''; $i < $len; $i++)
{
if ($str{$i} != $delimiter || ($quote_is_begin && $str{$i} == $delimiter))
{
if ($str{$i} == "(" && !$quote_is_begin)
{
$quote_is_begin = TRUE;
}
else if ($str{$i} == ")" && $quote_is_begin && $str{$i - 1} != '\\')
{
$quote_is_begin = FALSE;
}
$tmp .= $str{$i};
$in_delimiter = FALSE;
}
else if ($str{$i} == $delimiter && !$in_delimiter && !$quote_is_begin)
{
$in_delimiter = TRUE;
if (strlen($tmp) > 0)
{
$tab[] = $tmp;
}
$tmp = '';
}
}
if (strlen($tmp) > 0)
{
$tab[] = $tmp;
}
return ($tab);
}
$q = "select champ1,
champ2, to_char(champ3,'YYYYMM')
from (SELECT champ1, champ2 from table) vue_inline, table2
where ";
$tabQ = array();
preg_match("/^SELECT([a-zA-Z0-9_,\s()']+?)FROM/i", $q, $tabQ);
$tabQ = explode_parser($tabQ['1'], ',');
$tabQ = array_map('trim', $tabQ);
$strQ = implode('||', $tabQ);
$q = preg_replace("/^SELECT([a-zA-Z0-9_,\s()']+?)FROM/i", 'SELECT '.$strQ.' FROM', $q);
echo $q;
?> |
Partager