phpreplacewhitespacedigital-signature

Replace whitespace in php with %


I need to modify a default prestashop db query. I've tried the following:

public function ajaxProcessCheckProductName()
{
    if ($this->tabAccess['view'] === '1')
    {
        $search = Tools::getValue('q'); /* in this variable, i wish subsitute the space with % */
        $searchmod = str_replace(" ", "%", $search);
        $id_lang = Tools::getValue('id_lang');
        $limit = Tools::getValue('limit');
        $result = Db::getInstance()->executeS('
            SELECT DISTINCT pl.`name`, p.`id_product`, pl.`id_shop`
            FROM `'._DB_PREFIX_.'product` p
            LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
                ON (pl.`id_product` = p.`id_product` AND pl.`id_lang` = '.(int)$id_lang.')
            WHERE pl.`name` LIKE "%'.pSQL($searchmod).'%"
            GROUP BY pl.`id_product`
            LIMIT '.(int)$limit);
        die(Tools::jsonEncode($result));
    }
}

I wish replace the space in a variable, like kawasaki racing filter with %, so I can do a query on db with complete wildcards...

Example: a user search kawasaki racing filter, I wish replace it with kawasaki%racing%filter so, the query will be %kawasaki%racing%filter%...

Does the approach seem to be correct? It doesn't works right now :(

Can someone help me? Thanks in advance!


Solution

  • The most likely reason it doesn't work for you is that if you search the database for %kawasaki%racing%filter%, it will search for entries that contain all those words, in that order.

    My hunch is that you're expecting it to find entries that contain those three words anywhere in the string, but the way you're doing it means that they have to appear in the same order that they're entered in the query.

    You would be better off splitting the string into separate words, and searching for each one independently.

    eg

    ... WHERE field LIKE '%kawasaki%'
          AND field LIKE '%racing%'
          AND field LIKE '%filter%'
    

    This will search for records that contain all three words, but in any order. If you want to find any one of those words, rather than all three, then replace AND with OR.

    Also:

    In some cases, wildcard searches are useful, but MySQL (and other DBs; you didn't specify which DB system you're using) do have alternative options if you need to do a lot of text searching.

    The simplest option is simply to store your keywords in a separate table. You can think of them as tags. Then you can just query the exact word directly.

    If you need more than that, you could look into MySQL's 'Full Text Search' feature, which can be seen as a direct replacement to slow wildcard searching.

    But even that can be slow sometimes, so if you really want fast, check out Sphinx. It's a bit more work, but do it right and it will make your searches lightning fast.