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).
Change your regex into:
$query = "SELECT `x`, `y` FROM `test` ORDER BY `x` ASC, `z` ASC LIMIT 0, 10";
$query = preg_replace('~(ORDER BY|,)(\s+\S+)\s+(?=ASC|DESC)~iU', ' $1$2 COLLATE NOCASE ', $query);
echo $query,"\n";
Output:
SELECT `x`, `y` FROM `test` ORDER BY `x` COLLATE NOCASE ASC, `z` COLLATE NOCASE ASC LIMIT 0, 10
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.