Do you want to remove accents and leave lowerCase? If it is, the lower () function does this for you. After that, you have to run preg_replace for each word (otherwise it would only match if the two words were followed in both the post and < em> data_mysql ), then you have to do a explode . The findWords () function does this for you.
Do 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: Have you looked into the possibility of doing this on the Front End? This is my suggestion.