7 path.inc drupal_match_path($path, $patterns)
6 path.inc drupal_match_path($path, $patterns)

Check if a path matches any pattern in a set of patterns.

Parameters

$path: The path to match.

$patterns: String containing a set of patterns separated by \n, \r or \r\n.

Return value

Boolean value: TRUE if the path matches a pattern, FALSE otherwise.

3 calls to drupal_match_path()
block_block_list_alter in modules/block/block.module
Implements hook_block_list_alter().
DrupalMatchPathTestCase::testDrupalMatchPath in modules/simpletest/tests/path.test
Run through our test cases, making sure each one works as expected.
path_is_admin in includes/path.inc
Determines whether a path is in the administrative section of the site.

File

includes/path.inc, line 312
Functions to handle paths in Drupal, including path aliasing.

Code

function drupal_match_path($path, $patterns) {
  $regexps = &drupal_static(__FUNCTION__);

  if (!isset($regexps[$patterns])) {
    // Convert path settings to a regular expression.
    // Therefore replace newlines with a logical or, /* with asterisks and the <front> with the frontpage.
    $to_replace = array(
      '/(\r\n?|\n)/', // newlines
      '/\\\\\*/', // asterisks
      '/(^|\|)\\\\<front\\\\>($|\|)/', // <front>
    );
    $replacements = array(
      '|',
      '.*',
      '\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\2',
    );
    $patterns_quoted = preg_quote($patterns, '/');
    $regexps[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
  }
  return (bool) preg_match($regexps[$patterns], $path);
}

Comments

For example:

<?php
drupal_match_path
('my/path/here', 'my/path/*'); //returns true

drupal_match_path('my/path/here', 'not/my/path/*'); //returns false
?>

<?php
$path
= current_path();
$path_alias = drupal_lookup_path('alias', $path);
$patterns = '<front>' . PHP_EOL . 'node/n' . PHP_EOL . 'blog/*';

if (
drupal_match_path($path, $patterns) || drupal_match_path($path_alias, $patterns)) {
 
// Do things.
}
?>

See:
current_path()
drupal_lookup_path()

This function did not work as advertised for me. For example:

Does not handle aliases

NOTE drupal_get_path_alias(node/2292 ) == "degrees/masters"

Running drupal_match_path("node/2292", "degrees/masters" );
The outcome is FALSE

Running drupal_match_path("degrees/masters", "degrees/masters" );
The outcome is TRUE

Does not handle multiple patterns

Running drupal_match_path("node/2292", "degrees/masters\r\nnode/2292" );
The outcome is FALSE

Running drupal_match_path("degrees/masters", "degrees/masters\r\nnode/2292" );
The outcome is FALSE

In the end, I ditched this function and used a bastardized version of context_condition_path->match() from context_condition_path.inc.