可能重複:
Test if a regular expression is a valid one in PHP如何檢查給定的字符串是否有效正則表達式?
<?php
$subject = "PHP is the web scripting language of choice.";
$pattern = 'sssss';
if(preg_match($pattern,$subject))
{
echo 'true';
}
else
{
echo 'false';
}
?>
上面的代碼給我警告,因爲串$pattern
不是有效的正則表達式。
如果我通過有效的正則表達式,然後它工作正常.....
我怎麼能檢查$pattern
是有效的正則表達式?
在這裏看到正確的:http://stackoverflow.com/questions/7095238/an-invalid-regex-pattern –
,或者更好,這裏:http://stackoverflow.com/questions/362793/regexp-that-matches-valid-regexps – k102
或這一個:http://stackoverflow.com/questions/8825025/test-if-a-regular-expression-是一個有效的一個在PHP –