Re: case and accent - insensitive regular expression?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 8:31 AM -0400 7/13/08, Andrew Ballard wrote:
On Sat, Jul 12, 2008 at 10:29 AM, tedd <tedd.sperling@xxxxxxxxx> wrote:
 At 9:36 AM +0200 7/12/08, Giulio Mastrosanti wrote:

 Hi,
 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

 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,

 tedd

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);

?>

Andrew

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.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux