【发布时间】:2011-10-15 22:39:56
【问题描述】:
我正在尝试在 IF 语句中使用 preg_match,如果字符串包含一些不允许的模板函数,则返回 false。
以下是一些允许的示例模板函数:
{function="nl2br($value.field_30)"}
{function="substr($value.field_30,0,250)"}
{function="addslashes($listing.photo.image_title)"}
{function="urlencode($listing.link)"}
{function="AdZone(1)"}
这些与html等混合在一起。
现在,如果 regex 匹配代码格式但不包含允许的函数关键字之一,我希望此 preg_match 语句返回 true:
if (preg_match('(({function=)(.+?)(nl2br|substr|addslashes|urlencode|AdZone)(.+?)\})',$string)) {
// found a function not allowed
} else {
// string contains only allowed functions or doesn't contain functions at all
}
有人知道怎么做吗?
【问题讨论】:
-
不确定正则表达式是否是最好的工具。也许它们可以与老式的标记器和解析器结合使用:-?不过,无论该功能的潜在风险如何,这个问题都很好:)
-
我打算这样做,但为了保持代码干净,我想我先在这里要求一个简单的 if 语句解决方案。
标签: php regex preg-match