5

I have the following PHP code:

$search = "foo bar que";
$search_string = str_replace(" ", "|", $search);

$text = "This is my foo text with qué and other accented characters.";
$text = preg_replace("/$search_string/i", "<b>$0</b>", $text);

echo $text;

Obviously, "que" does not match "qué". How can I change that? Is there a way to make preg_replace ignore all accents?

The characters that have to match (Spanish):

á,Á,é,É,í,Í,ó,Ó,ú,Ú,ñ,Ñ

I don't want to replace all accented characters before applying the regex, because the characters in the text should stay the same:

"This is my foo text with qué and other accented characters."

and not

"This is my foo text with que and other accented characters."

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Felix Bernhard
  • 396
  • 6
  • 24

4 Answers4

3

The solution I finally used:

$search_for_preg = str_ireplace(["e","a","o","i","u","n"],
                                ["[eé]","[aá]","[oó]","[ií]","[uú]","[nñ]"],
                                $search_string);

$text = preg_replace("/$search_for_preg/iu", "<b>$0</b>", $text)."\n";
Felix Bernhard
  • 396
  • 6
  • 24
1

If you want to use the captured text in the replacement string, you have to use character classes in your $search variable (anyway, you set it manually):

$search = "foo bar qu[eé]"

And so on.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Alternatively, you could have some array that maps, say, `e` to `eé` and replace all occurrences of `vowel` with `"[" . $dict[$vowel] ."]"`. – Nic May 15 '15 at 12:32
1
$search = str_replace(
   ['a','e','i','o','u','ñ'],
   ['[aá]','[eé]','[ií]','[oó]','[uú]','[nñ]'],
   $search)

This and the same for upper case will complain your request. A side note: ñ replacemet sounds invalid to me, as 'niño' is totaly diferent from 'nino'

Saic Siquot
  • 6,513
  • 5
  • 34
  • 56
0

You could try defining an array like this:

$vowel_replacements = array(
    "e" => "eé",
    // Other letters mapped to their other versions
);

Then, before your preg_match call, do something like this:

foreach ($vowel_replacements as $vowel => $replacements) {
    str_replace($search_string, "$vowel", "[$replacements]");
}

If I'm remembering my PHP right, that should replace your vowels with a character class of their accented forms -- which will keep it in place. It also lets you change the search string far more easily; you don't have to remember to replaced the vowels with their character classes. All you have to remember is to use the non-accented form in your search string.

(If there's some special syntax I'm forgetting that does this without a foreach, please comment and let me know.)

Nic
  • 6,211
  • 10
  • 46
  • 69