note 107707 added to sqlite3.createfunction

From: Date: Tue, 28 Feb 2012 14:45:11 +0000
Subject: note 107707 added to sqlite3.createfunction
Groups: php.notes 
Request: Send a blank email to php-notes+get-186408@lists.php.net to get a copy of this message
In PHP 5.4 there will be a createCollation method to use your custom collation method, to be able to sort datasets using unicode, like this: <?php setlocale(LC_COLLATE, 'fr_FR.UTF-8'); $db->createCollation('PHP_COLLATE', 'strcoll'); $db->query('SELECT * FROM my_table ORDER BY name COLLATE PHP_COLLATE;'); ?> But until this cool feature becomes available, you'll have to do some tricks, like this for french: <?php function sqlite3_to_ascii($str, $charset = 'UTF-8') { // Don't process empty strings if (!trim($str)) return $str; // We only process non-ascii strings if (preg_match('!^[[:ascii:]]+$!', $str)) return $str; $str = htmlentities($str, ENT_NOQUOTES, $charset); $str = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str); $str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); $str = preg_replace('#&[^;]+;#', '', $str); return $str; } $db->createFunction('to_ascii', 'sqlite3_to_ascii', 1); $res = $db->query('SELECT * FROM test ORDER BY to_ascii(text);'); ?> This will convert non-ascii characters to ascii ones before collation. In fact this won't work with non-latin languages, but for latin-languages it's better than nothing. Please note that this will slow down about 1.8 times the query (tested on a 10.000 rows table). ---- Server IP: 89.31.146.176 Probable Submitter: 109.190.6.11 ---- Manual Page -- http://www.php.net/manual/en/sqlite3.createfunction.php Edit -- https://master.php.net/note/edit/107707 Del: integrated -- https://master.php.net/note/delete/107707/integrated Del: useless -- https://master.php.net/note/delete/107707/useless Del: bad code -- https://master.php.net/note/delete/107707/bad+code Del: spam -- https://master.php.net/note/delete/107707/spam Del: non-english -- https://master.php.net/note/delete/107707/non-english Del: in docs -- https://master.php.net/note/delete/107707/in+docs Del: other reasons-- https://master.php.net/note/delete/107707 Reject -- https://master.php.net/note/reject/107707 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186408) next »