fnmatch()

fnmatch() is a matching native function: it applies the shell wildcard patterns to a string, and returns if it matches or not.

Wildcards include:

  • * for anything

  • ? for zero or one character

  • [] for a range of characters

  • ! to negate characters in a bracket syntax

  • `` `` as the escape character

The search is case sensitive.

This function is useful to emulate a common system of search.

Simpler search tools include str_contains() and strpos(), and more complex include preg_match().

<?php

    $message = 'PHP rocks';
    if (fnmatch('*r[oi]cks', $message)) {
      echo 'But, of course...';
    }

?>

Documentation

See also Understanding and Implementing PHP’s fnmatch() Function.

Related : str_contains(), strpos, Preg_match, Star *, Bang !, Square Brackets, Question Mark ?, Backslash