Re: case and accent - insensitive regular expression?

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

 





First of all thank you all for your answers, and thank you for your time

and yes Tedd, my question was quite ambiguous in that point.

Andrew is right, i don't want to change in any way the list of keys I show in the result, I just want to find the way to higlight the matching words, regardless of their accent variations.

So I think his Andrew's suggestion could be a good solution, and I'll try it ASAP...

let me se if i correctly understood:

$search = preg_quote($word); -- quotes chars that could be intrepreted like regex special chars

$search = str_replace('e', '[eèéêë]', $search); -- trasforms i.e. cafe in caf[eèéêë], so matches all the accented variations

return preg_replace('/\b' ... -- replaces all the occurences adding the tags, you use \b as word boundary, right?

it seems a fine soultion to the problem!

the only thing i must add is, befor calling highlight_search_terms, to 'normalize' the word string ( the word used for the search) to transform it removing the accentated versions of the chars:

$word = preg_replace('[èé]{1}','e',$word);
$word = preg_replace('[à]{1}','a',$word);

that because also the search string could contain an accented char, and this way I avoid to perform str_replace in the highlight_search_terms function for every combination of accented chars

well, i think I'm on the good way now, unfortunately I have some other urgent work and can't try it immediately, but I'll let you know :)

thank you!

     Giulio



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


--



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