Quote:
|
Originally Posted by MRMAN
Hello,.
I am making a book site and currently in the database are book titles likes "John's Trip" and "Peter's new bike".
I have a search function where the customer can type in "Peter's" and it will bring up everything with "Peter's" in it.
But i was wonderind is there any way i can search for "Peters" and "Johns".
|
kinda depends on what your current select looks like.
i imagine your select looks like
SELECT foobar FROM yourtable WHERE title LIKE '%Peter\'s%'
which is probably build like
PHP Code:
$sql="SELECT foobar FROM yourtable WHERE title LIKE '%". $_POST['searchterm'] ."%'"
one option would be to replace the ' by % like
PHP Code:
$sql="SELECT foobar FROM yourtable WHERE title LIKE '%". str_replace("'", '%', $_POST['searchterm']) ."%'"
but this would also match a title like 'Peter knows'
another, more stringent but less performant option would be
PHP Code:
$clause = "title LIKE '%". $_POST['searchterm'] ."%'";
if (strpos($_POST['searchterm'], "'") !== False){
$clause .= " OR title LIKE '%". str_replace("'", '', $_POST['searchterm']) ."%'";
}
$sql="SELECT foobar FROM yourtable WHERE ". $clause;
and there is probebly also a regex possebility...