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:
<?php
$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:
<?php
$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..