0

I realize a search bar in my database. I explain I have two users in my database: "James Da" and "Paul Mo" I want that when the user searches for "James" this will show "James Da" but I also want that when he looks for "Da" it will show "James Da, and when he looks for just "ames", it will show "James Da" ...

My function only realizes the first case:

public function userSearch()
{
    $users = [];
    $request = Request::createFromGlobals();
    $name = $request->query->get('name');

    $directoryService = $this->get('etiam_nexus_directory.service.directory');
    $localEstablishment = $directoryService->getLocalEstablishment();
    $allUsers = $directoryService->getAllUsers($localEstablishment, $_GET);

    for ($i = 0; $i <= count($allUsers); $i++) {
        $user = $allUsers[$i];
        if (substr(strtolower($user->getUserFullName()), 0, strlen($name)) === strtolower($name)) {
            $users[] = '@' . $user->getUseruniqname() . ":server";
        }
    }

    if(count($users) == 0) {
        return JsonResponse::create(
            array(
                'success' => false,
                'research' => $name
                )
        );
    }

    return JsonResponse::create(
        array(
                'success' => true,
                'research' => array (
                    'name' => $users
                )
        )
    );
}

Should I use another condition taking into account the space?

user9099802
  • 243
  • 2
  • 11
  • 19
  • Do you have a link to publicly accessible API? – Alex.Barylski Jan 04 '18 at 16:29
  • 1
    You could filter using PHP but it'd be a lot easier with the directory service handling the LIKE/WILDCARD filter, IMO. – Alex.Barylski Jan 04 '18 at 16:30
  • Thanks for your answer. I found the response : `if (strpos(strtolower($user->getUserFullName()), strtolower($name)) !== false) { $users[] = '@' . $user->getUseruniqname() . ":server"; }` – user9099802 Jan 04 '18 at 16:32
  • @user9099802 instead of making both strings lowercase and compare them with `strpos()` afterwards, you can use `stripos()` instead, which will compare both strings case insensitively. – Philipp Maurer Jan 04 '18 at 16:36
  • Possible duplicate of [How do I check if a string contains a specific word?](https://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word) – William Perron Jan 04 '18 at 16:38

1 Answers1

0

You currently only check for the first case:

substr(strtolower($user->getUserFullName()), 0, strlen($name)) === strtolower($name)

substr(strtolower($user->getUserFullName()), 0, strlen($name)) will cut the first part out of the username, that has at most as many characters as the name that is searched for. Since you then make an equal check you only check, whether or not the full username starts with the name, that the user just typed in.

Instead, you could simpl use stripos

if (stripos($user->getUserFullName(), $name) !== FALSE) {
    $users[] = '@' . $user->getUseruniqname() . ':server';
}

This will check, if the string that the user entered is part of the users name, while being case insensitve. With rules like 'ames' will find 'James Do', it seems to be accurate.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25