lundi 9 novembre 2015

Inserting a string into an SQL query to change the order with PHP

I want to insert a 'COLLATE NOCASE' before each ORDER BY clause of an SQLite query. This is what I have so far:

     $string = preg_replace('~(ORDER\s+BY\s+`?.+`?)\s+([ASC|DESC]?)~iU', '$1 COLLATE NOCASE $2', $string);

The output is:

SELECT `x`, `y` FROM `test` ORDER BY `x` COLLATE NOCASE ASC, `z` ASC LIMIT 0, 10

The first instance is matched and replaced. The second instance is not replaced (because of the ORDER BY clause in the pattern).

Aucun commentaire:

Enregistrer un commentaire