Re: case and accent - insensitive regular expression?
| From: | tedd | Date: | Sun, 13 Jul 2008 12:50:27 +0000 |
| Subject: | Re: case and accent - insensitive regular expression? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-276722@lists.php.net to get a copy of this message | ||
At 8:31 AM -0400 7/13/08, Andrew Ballard wrote:
On Sat, Jul 12, 2008 at 10:29 AM, tedd <tedd.sperling@gmail.com> wrote:Andrew: You may be right -- it's ambiguous now that I review it again. He does say search and replace but I'm not sure if that's what he really wants. It looks more like search with one string and highlight all like-strings. Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.comAt 9:36 AM +0200 7/12/08, Giulio Mastrosanti wrote:I may be mistaken (and if I am, then just ignore this as ignorant rambling), but I don't think he's wanting to replace the accented characters in the original string. I think he's just wanting the pattern to find all variations of the same string and highlight them without changing them. For example, his last paragraph would look like this: [quote] now my problem is to find a way ( I imagine with some kind of regular expression ) to achieve in php a search and replace accent-insensitive, so that i can find the word '<span class="keysearch">cafe</span>' in a string also if it is '<span class="keysearch">café</span>', or '<span class="keysearch">CAFÉ</span>', or '<span class="keysearch">CAFE</span>', and vice-versa. [/quote] The best I can think of right now is something like this: <?php function highlight_search_terms($word, $string) { $search = preg_quote($word); $search = str_replace('a', '[aàáâãäå]', $search); $search = str_replace('e', '[eèéêë]', $search); /* repeat for each possible accented character */ return preg_replace('/\b' . $search . '\b/i', '<span class="keysearch">$0</span>', $string); } $string = "now my problem is to find a way ( I imagine with some kind of regular expression ) to achieve in php a search and replace accent-insensitive, so that i can find the word 'cafe' in a string also if it is 'café', or 'CAFÉ', or 'CAFE', and vice-versa."; echo highlight_search_terms('cafe', $string); ?> AndrewHi, I have a php page that asks user for a key ( or a list of keys ) and then shows a list of items matching the query. every item in the list shows its data, and the list of keys it has ( a list of comma-separated words ) I would like to higlight, in the list of keys shown for every item, the words matching the query, this can be easily achieved with a search and replace, for every search word, i search it in the key list and replace it adding a style tag to higlight it such as for example to have it in red color: if ( @stripos($keylist,$keysearch!== false ) { $keylist = str_ireplace($keysearch,'<span style="color: #FF0000">'.$keysearch.'</span>',$keylist); } but i have some problem with accented characters: i have mysql with character encoding utf8, and all the php pages are declared as utf8 mysql in configured to perform queries in a case and accent insensitive way. this mean that if you search for the word 'cafe', you have returned rows that contains in the keyword list 'cafe', but also 'café' with the accent. ( I think it has to do with 'collation' settings, but I'm not investigating at the moment because it is OK for me the way it works ). now my problem is to find a way ( I imagine with some kind of regular expression ) to achieve in php a search and replace accent-insensitive, so that i can find the word 'cafe' in a string also if it is 'café', or 'CAFÉ', or 'CAFE', and vice-versa. hope the problem is clear and well-explained in english, thank you for any tip,Giulio: Three things: 1. Your English is fine. 2. Try using mb_ereg_replace() http://www.php.net/mb_ereg_replace Place the accents you want to change in that and change them to whatever you want. 3. Change: <span style="color: #FF0000">'.$keysearch.'</span>' to <span class="keysearch">'.$keysearch.'</span>' and add .keysearch { color: #FF0000; } to your css. Cheers, teddGiulio