php - Change str_replace to preg_replace in SELECT WHERE


I want to create a search engine for all posts on my blog. Initially, my code is:

    $search = mysqli_real_escape_string($connection, $_POST['search']);
    $search = str_replace('\'', '', $search);
    $search = str_replace('-', '', $search);
    $search = str_replace('.', '', $search);

    $sqlsearch = $connection->query("SELECT * FROM table
    WHERE replace(replace(replace(title, '\'', ''), '-', ' '), '.', '') LIKE '%{$search}%' GROUP BY title");

The above script will collect the data in the filtered 'title' column first using 'str replace' and work properly. But I need to create more lists to filter more queries.

So I thought, maybe it could be replaced with 'preg_replace', but not sure yet to apply to my blog.

Could the above script be replaced by this code:

    $search = mysqli_real_escape_string($connection, $_POST['search']);
    $search = preg_replace('/[^\p{L}\p{N}]/u', '', $search);

    $sqlsearch = $connection->query("SELECT * FROM table
    WHERE preg_replace('/[^\p{L}\p{N}]/u', '', title) LIKE '%{$search}%' GROUP BY title");

Please give me the most possible input for me to do. Thank you..

People are also looking for solutions to the problem: Get echo from php into javascript (separate files)


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.