Jump to content

Santizing search value with selectorValue


Recommended Posts


I have a site search working correctly on a project, however I seem to be having issue when the search term contains characters such as question marks or brackets.

I'm doing the following... 

$q = $input->get->text('q');

if($q) {
  $input->whitelist('q', $q);
  $q = $input->get->selectorValue('q');

$matches = "search_cache~={$q}, sort=-publish_date";

This works correctly for everything except searches that include characters such as ? or ( )

So for example a searches for the following (where the page title is "example title?")

  1. example title
  2. example title?

The first one works as expected and the second returns 0 results.

As the titles are very specific for a user to search, I need to make sure the search works with these kind of characters.

How can I make sure search works with these kind of characters?





Link to comment
Share on other sites

If you search using ~=, you're performing a fulltext search (aka natural language search) that only looks for words. If you need to be able to search for non-word characters too, you'll have to use %= (LIKE).

For fulltext, you'll likely need to clear out any non-word, non-digit, non-whitespace and non-connecting characters, e.g. with '/[^\p{N}\p{L}\p{Pc}\p{Zs}]/u'.

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...