here is an annotation to my note from 28-Apr-2005 03:52, due to the welcome contribution of dipesh khakhkhar:
Here is a small function to determine whether a string is a preg expression.
Please note, that a dot '.' character in a regexp may match any character, including a dot, thus a string containing a dot may well be interpreted as an ordinary string, or a regexp.
function preg_ispreg($str)
{
$prefix = "";
$sufix = "";
if ($str[0] != '^')
$prefix = '^';
if ($str[strlen($str) - 1] != '$')
$sufix = '$';
$estr = preg_replace("'^/'", "\\/", preg_replace("'([^/])/'", "\\1\\/", $str));
if (@preg_match("/".$prefix.$estr.$sufix."/", $str, $matches))
return strcmp($str, $matches[0]) != 0;
return true;
}