Highlight term searched in bold with or without accent

Asked

Viewed 600 times

2

Hello friends would love your help a lot. I am trying to highlight the words typed in bold in the search. the code below works only if you type "Brazilian selection" more if you type "Soccer selection" nothing happens. I appreciate the help.. hugs..

//$post = "selecao de futebol"; //não funciona
$post = "selecao brasileira"; //funciona

$dados_mysql = "seleção brasileira de futebol";

$post = preg_replace('/[aáàâãäeéèêiíìoóòôõöuúùücç]/','(a|á|à|â|ã|ä|e|é|è|ê|i|í|ì|o|ó|ò|ô|õ|ö|u|ú|ù|ü|c|ç)',$post);
$resultado = preg_replace("/($post?)/i","<b>\\0</b>",$dados_mysql);

echo "$resultado";

2 answers

5


Do you want to remove accents and let lowercase? If it is, the Lower() function does it for you. After that, you have to run preg_replace for every word (otherwise it would only give match if the two words were followed both in the post when in the dados_mysql), then you have to make a explode. The findWords() function does this for you.

Do it like this:

function lower($string) {
    $string = mb_strtolower($string, 'UTF-8');
    $string = preg_replace('/[`´^~\'"]/', null, iconv( 'UTF-8', 'ASCII//TRANSLIT', $string ) );
    return $string;
}

function findWords($string1, $string2) {
    $stringLow1 = lower($string1);
    $stringLow2 = lower($string2);

    $stringLow1 = explode(" ", $stringLow1);

    foreach ($stringLow1 as $string) {
        $stringLow2 = preg_replace("/($string?)/i","<b>\\0</b>", $stringLow2);
    }

    $string2 = explode(" ", $string2);
    $stringLow2 = explode(" ", $stringLow2);

    foreach ($stringLow2 as $key => $string) {
        if (preg_match("/<b>/", $string)) {
            $string2[$key] = '<b>' . $string2[$key] . '</b>';
        }
    }

    $string2 = implode(" ", $string2);

    return $string2;
}

$dados_mysql = "seleção brasileira de futebol";
$digitado = "seleção de futebol";

echo findWords($digitado, $dados_mysql);

Note: analyzed the possibility of doing this in the Front-end? This is my suggestion.

  • Too big to put in a comment, I edited the answer.

  • 1

    Wow.. perfect.. It’s already the 7°day that I try to do this and you solve in a few minutes. you are to congratulations.. hugs..

2

You can try developing using the function strripos:

$termo_pesquisa     = 'ababcd';
$buscar             = 'ab';
$pesquisar          = strripos($termo_pesquisa, $buscar);

if ($pesquisar === false) {
    echo "Sinto muito, nós não encontramos ($termo_pesquisa) em ($buscar)";
} else {
    echo "Parabéns!\n";
    echo "Nós encontramos a <strong>($buscar)</strong> em ($termo_pesquisa) na posição ($pesquisar)";
}

If there are any questions you can read the documentation as well: http://php.net/manual/en/function.strripos.php

  • Opa André ok. thanks for the answer. more the problem is that I’m using an autocomplete with this my code. more thank you anyway..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.