Skip to content

Instantly share code, notes, and snippets.

@aerith
Created October 25, 2010 08:14
Show Gist options
  • Save aerith/644591 to your computer and use it in GitHub Desktop.
Save aerith/644591 to your computer and use it in GitHub Desktop.
文字列が正規表現パターンかどうかをそれっぽく判別する関数。
<?php
/*
* 文字列が正規表現パターンかどうかをそれっぽく判別する関数。
*/
function regexpUses($pattern)
{
// See: http://php.net/manual/reference.pcre.pattern.modifiers.php
// See: http://php.net/manual/regexp.reference.delimiters.php
if (preg_match('@^([^\s\\\\a-z0-9])(?:.*?)\1(?:[imsxeADSUXJu]*)?$@mD', $pattern)) return true;
if (preg_match('@^\{(?:.*?)\}(?:[imsxeADSUXJu]*)?$@mD', $pattern)) return true;
if (preg_match('@^\[(?:.*?)\](?:[imsxeADSUXJu]*)?$@mD', $pattern)) return true;
if (preg_match('@^\<(?:.*?)\>(?:[imsxeADSUXJu]*)?$@mD', $pattern)) return true;
if (preg_match('@^\((?:.*?)\)(?:[imsxeADSUXJu]*)?$@mD', $pattern)) return true;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment