Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
Highlighting an accented search term using preg_replace

I've been trying to bold a search term in a sentence. If the sentence is Engliš is spoken wörldwide. If my search term is spoken world I want to get Engliš is <b>spoken wörld</b>wide.

I've used this function:

function highlightWords($text, $searchTerm){
   $corr = ['a' => '[aäâ]', 'o' => '[oöòóôõ]', 'c' => '[cç]', 's' => '[şśšșŝ]', 'y' => '[ýÿŷȳy]', 'o' => '[ôöòóøōoõ]', 'n' => '[ñńňn]',  'u' => '[üu]'];
   $key = preg_quote($searchTerm);
   $pattern = '/' . strtr($key, $corr) . '/iu';
   $text = preg_replace($pattern, '<b>$0</b>', $text);
   return $text;
}

It supposed to work, but I get really strange behavior. Few examples are:

Text is Sygmaý çykdy deşdi-sähra düzünden (sorry for the weird sentence). When $searchTerm is duz it perfectly works, I get Sygmaý çykdy deşdi-sähra <b>düz</b>ünden. If I change search term to sahra, the function returns just plain Sygmaý çykdy deşdi-sähra düzünden.

Works with cykdy and çykdy.

But doesn't work with neither sygmay nor sygmaý. But works with Sygmaý with capital letter.

What should I need to fix in order to get highlighted search term in all scenarios?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There are a couple of problems in the function, specifically in the $corr array.

First, there are two "o" sections, and they are different. Those need to be combined. Second, the unaccented letter must be in the array for each letter. s does not have this, the missing "s" is what is causing this particular failure.

Fixed function:

function highlightWords($text, $searchTerm)
{
    $corr    = [
        'a' => '[aäâ]', 
        'o' => '[oöòóôõøō]', 
        'c' => '[cç]', 
        's' => '[sşśšșŝ]', 
        'y' => '[yýÿŷȳ]', 
        'n' => '[nñńň]', 
        'u' => '[uü]'
    ];
    $key     = preg_quote($searchTerm);
    $pattern = '/' . strtr($key, $corr) . '/iu';
    $text    = preg_replace($pattern, '<b>$0</b>', $text);
    return $text;
}

$input = 'Sygmaý çykdy deşdi-sähra düzünden';
$term  = 'sahra';

$expected = 'Sygmaý çykdy deşdi-<b>sähra</b> düzünden';

$highlighted = highlightWords($input, $term);

assert($highlighted == $expected, 'Term should be marked bold');
echo $highlighted . PHP_EOL;
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda