Entity Query using conditions that ignore special characters I have an entity query where I search the body for user provided text.
I'm performing the query like so

$searchterm = 'test';
    $query = \Drupal::entityQuery('node')
            -&gt;condition('status', NODE_PUBLISHED)
            -&gt;condition('body', $searchterm, 'CONTAINS');


This gives me the results I need, but what if I run into a situation where the body contains the text 'test-1' but I want searches for 'test1' to bring up that result.

Is there anyway that I can write a query that ignores all special characters in the field I'm searching?
Drupal does not supply a solution to your problem out of the box.
However you can create a list of all the permutations and create a condition for each of them.



Code snippet

$searchterm = 'test-1_2 3';

$query = \Drupal::entityQuery('node')
  -&gt;condition('status', NODE_PUBLISHED);

// permutation generator function.
$permutations = function($source_term) {
  $result = [$source_term];
  $unprocessed_terms = [$source_term];
  $regex = '/[\W_]/'; // Selects all the non-alphanumeric characters and _

  while (!empty($unprocessed_terms)) {
    $unprocessed_term = array_pop($unprocessed_terms);
    $matches = [[]];

    // By using PREG_OFFSET_CAPTURE we get the position of the match.
    preg_match_all($regex, $unprocessed_term, $matches, PREG_OFFSET_CAPTURE);
    foreach($matches[0] as list($match, $pos)) {
      $result[] = $unprocessed_terms[] = substr_replace($unprocessed_term, '', $pos, 1);
    }
  }

  // the above algorithm will create duplicates.
  return array_unique($result);
};

// Create an OR condition group that contains a 
// condition for each permutation term.
$query-&gt;condition($or = $query-&gt;orConditionGroup());
foreach ($permutations($searchterm) as $term) {
  $or-&gt;condition('body', $term, 'CONTAINS');
}

$nids = $query-&gt;execute();