0

I'm trying to do a search engine where I write in a textbox, for example, "Mi" and it selects and shows "Mike Ross". However it's not working with spaces. I write "Mike" and I get "Mike Ross", but when I write "Mike " I get "Mike Ross" (no bold).

The same is happening with accents. So I write "Jo" and the result is "João Carlos". If I write "Joa", the result is "João Carlos" (without any bold part). I want to ignore the accents while writing but still display them in the results.

So this is my script after the SELECT:

 while($row = $result->fetch_array()) {
        $name = $row['name'];
        $array = explode(' ',trim($name));
        $array_length = count($array);

        for ($i=0; $i<$array_length; $i++ ) {
            $letters = substr($array[$i], 0, $q_length);

            if (strtoupper($letters) == strtoupper($q)) {
                $bold_name = '<strong>'.$letters.'</strong>';
                $final_name = preg_replace('~'.$letters.'~i', $bold_name, $array[$i], 1);
                $array[$i] = $final_name; 
        } 
         array[$i] = array[$i]." ";
        }
    foreach ($array as $t_name) { echo $t_name;
}

Thank you for your help!

Penny
  • 253
  • 1
  • 5
  • 15

1 Answers1

0
if (strtoupper($letters) == strtoupper($q))

This will never evaluate to "true" with spaces since you're removing spaces from the matchable letter set with explode(' ', trim($name), effectively making any value of $q with a space unmatchable to $letters

Here's a quick example that does what I think you're looking for

<?php

$q = "Mike "; // User query
$name = "Mike Ross"; // Database row value

if(stripos($name, $q) !== false) // Case-insensitive match
{
    // Case-insensitive replace of match with match enclosed in strong tag
    $result = preg_replace("/($q)/i", '<strong>$1</strong>', $name);
    print_r($result);
}

// Result is 
// <strong>Mike </strong>Ross  

From what I can tell (a quick google for "replace accented characters PHP"), you're kind of out of luck with that one. This question provides a quick solution using strtr, and this tip uses a similar method with str_replace.

Unfortunately, these rely on predefined character sets, so incoming accents you haven't prepared for will fail. You may be better off relying on users to enter the special characters when they search, or create a new column with a "searchable" name with the accented characters replaced as best as you can, and return the real name as the "matched" display field.

One more Note

I found another solution that can do most of what you want, except the returned name will not have the accent. It will, however, match the accented value in the DB with a non-accented search. Modified code is:

<?php

$q = "Joa";
$name = "João Carlos";

$searchable_name = replace_accents($name);

if(stripos($searchable_name, $q) !== false)
{
    $result = preg_replace("/($q)/i", '<strong>$1</strong>', $searchable_name);
    print_r($result);
}

function replace_accents($str) {
    $str = htmlentities($str, ENT_COMPAT, "UTF-8");
    $str = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/','$1',$str);
    return html_entity_decode($str);
}
Community
  • 1
  • 1
phatskat
  • 1,797
  • 1
  • 15
  • 32
  • Thank you! That solved the spaces problem. Any ideas do solve the accentuation one? – Penny Dec 31 '14 at 16:28
  • Updated answer with some further reading - the solutions aren't pretty but plenty of people have come up with solutions for this in some form. – phatskat Dec 31 '14 at 16:41
  • @Penny Updated with a semi-working accent matcher. The big note being that I'm not sure how to get the accent *back on* the return string. – phatskat Dec 31 '14 at 16:51