function db_like

Escapes characters that work as wildcard characters in a LIKE pattern.

The wildcard characters "%" and "_" as well as backslash are prefixed with a backslash. Use this to do a search for a verbatim string without any wildcard behavior.

You must use a query builder like db_select() in order to use db_like() on all supported database systems. Using db_like() with db_query() or db_query_range() is not supported.

For example, the following does a case-insensitive query for all rows whose name starts with $prefix:

$result = db_select('person', 'p')
  ->fields('p')
  ->condition('name', db_like($prefix) . '%', 'LIKE')
  ->execute()
  ->fetchAll();

Backslash is defined as escape character for LIKE patterns in DatabaseCondition::mapConditionOperator().

Parameters

$string: The string to escape.

Return value

The escaped string.

Related topics

16 calls to db_like()
Combine::opContains in drupal/core/modules/views/lib/Drupal/views/Plugin/views/filter/Combine.php
Combine::opEndsWith in drupal/core/modules/views/lib/Drupal/views/Plugin/views/filter/Combine.php
Combine::opNotEnds in drupal/core/modules/views/lib/Drupal/views/Plugin/views/filter/Combine.php
Combine::opNotLike in drupal/core/modules/views/lib/Drupal/views/Plugin/views/filter/Combine.php
Combine::opNotStartsWith in drupal/core/modules/views/lib/Drupal/views/Plugin/views/filter/Combine.php

... See full list

File

drupal/core/includes/database.inc, line 483
Core systems for the database layer.

Code

function db_like($string) {
  return Database::getConnection()
    ->escapeLike($string);
}