Search code examples
phpjqueryjquery-tokeninput

jQuery Tokeninput doesn't recognize first letters of a word


I'm using the jQuery Tokeninput autocomplete plugin from http://loopj.com/jquery-tokeninput/

I've built it into my application and I'm using a server-side script to get all the results. When I try it with the php-script from the Tokeninput-Demo, it works fine. But when I use my php script as a source, it doesn't recognize the first letter of a word. For instance, if I'm searching for "Marc Fisher" and I type "Marc" it doesn't find anything, but when I type "arc" then it works. The same goes for surnames.

Here is my php-script:

    $search             = $_GET['q'];
    $friends            = array(
    0   => array('name' => 'Marc Fisher', 'id' => '111'),
    1   => array('name' => 'Thomas Mann', 'id' => '222'),
    2   => array('name' => 'Jon Applebaum', 'id' => '333')
    );
    $searched_friends   = array();
    foreach ($friends as $friend) {
        $check          = strpos($friend['name'], $search);
        if ($check !== false) {
            array_push($searched_friends, $friend);
        }
    }
    echo(json_encode($searched_friends));

The weird thing is, if I type "Mar" directly in the URL for the GET, then it works and outputs:

[{"name":"Marc Fisher"}]

I've searched everywhere and tried everything I could think of - but to no avail. Do you know what's wrong?


Solution

  • I got it to work but I don't know why. Basically, I switched the strpos with a preg_match. Now it works.