PHP Search system ignore capital letters and add keywords

2

I have a simple search engine that looks up values in the DB and displays it on the page. In the table I have a column called keywords where the words I leave as a search parameter are registered. I need two things. The first is that when typing the word with capital or small letter it understands the same way without that I have to register both in the DB. The second thing is that he adds the words to the search. For example: I registered the words in the table in the following order; house roof construction. If I type house roof does not return anything to me, only if I search for one of the two. I want him to look for both.

Code

$busca = trim($_POST['busca']);
$sql = mysqli_query($conn, "SELECT * FROM pesquisa_clientes WHERE palavraschaves LIKE '%".$busca."%' ORDER BY nome");
$numRegistros = mysqli_num_rows($sql);
if ($numRegistros != 0) {


echo "<h4 class='result'>Resultados para: <b> " . $busca . "</b></h4><br />";

        while ($usuario = mysqli_fetch_object($sql)) {

echo "<div id='resultados'>";
echo "<img src='images/clientes/".$usuario->logo."' alt='Foto de exibição' /><br />";
echo utf8_encode("<h4><b> " . $usuario->nome . "</b></h4><br />");
echo utf8_encode("<p><span><b> " . $usuario->subcategoria . "</b></span></p><br /><br />");
echo utf8_encode("<p><b>Bairro:</b> " . $usuario->bairro . "</p><br />");
echo "<p><b>Telefone:</b> " . $usuario->telefone . "</p><br />";
echo "<a href='". $usuario->link_cliente. "'><b>Saiba Mais</b></a><br /><br />";
echo "</div>";
}
} else {
        echo "<h4 class='result'>Nada foi encontrado com a palavra:<b> ".$busca."</b></h4>";
}
    ?>
    
asked by anonymous 10.06.2017 / 04:05

2 answers

4
  • Regarding capital letters your question is not very clear. I will assume that the words in the bank are all lowercase and the user types uppercase or lowercase. In this case you just have to use the strtolower function that converts all characters to lowercase

DEMO

$busca = trim($_POST['busca']);
//para remover os espaços duplicados.
$busca = preg_replace('/\s(?=\s)/', '', $busca);
//tudo em minusculas
$busca = strtolower($busca)

//quando uma pessoa digitar umas palavras, elas são explodidas pelo espaço
$exp = explode(" ", $busca); // separando pelo espaço

//e em seguida são implodidas pela | (barra vertical)
$imp = implode("|", $exp); // unindo os valores pela |

//a consulta é como segue
SELECT * FROM pesquisa_clientes WHERE palavraschaves REGEXP '$imp' ORDER BY nome
  

A | (vertical bar) is required for the use of REGEXP, it symbolizes the OR "or", that is, it searches between values separated by | (vertical bar).

To convert accents you can use the function

Function TirarAcento( $texto ) 
{
    $array1 = array(   "á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç"); 

    $array2 = array(   "a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c"); 

    return str_replace( $array1, $array2, $texto ); 
}  

 $busca = trim($_POST['busca']);
 $busca = preg_replace('/\s(?=\s)/', '', $busca);

 $busca = strtolower($busca);

 $busca = TirarAcento($busca);
 ............................
 ............................
 ............................
  

call the function TirarAcento after the function strtolower

Want to know why? mouse over the area below yellowish

  

Because the function TirarAcento is doing the change of letters accented only to lowercase, therefore it is indispensable to place the search terms first in lowercase with the function strtolower

    
10.06.2017 / 07:57
0

On uppercase and lowercase letters, I think the best way is for you to define how you are going to write the words in the bank, if it is a capital letter, for example, when reading the words that the user typed, you transform that string in capital letters with strtoupper string). The second part of the problem, you can break the string that the user typed with the explode, done that, you will have an array with the words captured, then just use the implode and separate the values of the arrays by commas in your select. / p>

It would look something like this:

$busca = trim($_POST['busca']);
//transformo tudo em maiúsculo
$busca = strtoupper($busca)
//quebro a string nos espaços, e gravo no array
$array= explode(" ", $busca)
//where IN (valor1, valor2,valor3), uso o implode para coloca a virgula após cada valor do array
$sql = mysqli_query($conn, "SELECT * FROM pesquisa_clientes WHERE palavraschaves in ('.implode(',', array_map('intval', $array)).') ORDER BY nome");

I do not know if this is working because I have not tried it, but the idea is this

    
10.06.2017 / 05:56