preg_match() 语句中的未知修饰符
经过两次尝试(感谢 Stack Overflow),我终于让我的正则表达式 url 正常工作,并按照我想要的不同方式将特定内容列入黑名单。然而,它不能正常工作,虽然是的;它确实将这些单词列入黑名单并阻止它们输入数据库,它会抛出一个错误:
PHP 警告:preg_match() [function.preg-match]:未知修饰符 '\\' 在 C:\wamp\www\anonpost\index.php 第 324 行,引用地址: http://localhost/anonpost/index.php
这是我的代码:
$disallowedWords1 = array(
'offensive','words'
);
foreach ($disallowedWords1 as $word1) {
if (preg_match("/\b$word1\b/i", $entry)) {
die('The word or phrase ' . $word . ' is not allowed...');
}
}
$urlRegex = '(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*';
if (preg_match($urlRegex, $entry)) {
die('The word or phrase ' . $word . ' is not allowed...');
}
我查看了一些帖子网站上类似,他们几乎都指出没有分隔符,我有那些,所以我真的不知道它可能是什么。任何帮助将不胜感激:)
(对于错误的示例,其中有三个[三个不同的 preg_match() 语句,每个语句都略有不同,主要是它找到文本的方式]尝试发布,这不是问题,它只是让它看起来很混乱: http://aviatex14.co.uk/anonpost/ )
After 2 attempts (thanks to Stack Overflow) I finally got my regex url to work and blacklist specific things in different ways as I wanted. However, it doesn't work properly, whilst yes; it does blacklist the words and prevent them being entered into the database, it throws up an error:
PHP Warning: preg_match() [function.preg-match]: Unknown modifier
'\\' in C:\wamp\www\anonpost\index.php on line 324, referer:
http://localhost/anonpost/index.php
And here's my code:
$disallowedWords1 = array(
'offensive','words'
);
foreach ($disallowedWords1 as $word1) {
if (preg_match("/\b$word1\b/i", $entry)) {
die('The word or phrase ' . $word . ' is not allowed...');
}
}
$urlRegex = '(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*';
if (preg_match($urlRegex, $entry)) {
die('The word or phrase ' . $word . ' is not allowed...');
}
I looked at some posts that were similar on the site, they all pretty much made the point that there were no delimiter's, well I have those, so I am really lost as to what it could be. Any help would be appreciated :)
(For an example of the error's, of which there are three [three different preg_match() statements, each only marginally different, mainly the way it finds the text] try posting, it's not a problem, it just makes it look messy: http://aviatex14.co.uk/anonpost/ )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
转义所有
/
或使用另一个正则表达式分隔符,即~
或#
此外,您的正则表达式可以缩短:
Escape all the
/
or use another regex delimiter, i.e.~
or#
moreover, your regex could be shortened:
在正则表达式中使用主题标签,即:(
您可能必须删除开头的斜杠)
Use hashtags in the Regex, ie:
(You may have to remove that beginning slash)