preg_match() 语句中的未知修饰符

发布于 2024-12-10 20:48:50 字数 1507 浏览 0 评论 0原文

经过两次尝试(感谢 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念满溢 2024-12-17 20:48:50

转义所有 / 或使用另一个正则表达式分隔符,即 ~#

if (preg_match("#$urlRegex#", $entry)) {

此外,您的正则表达式可以缩短:

$urlRegex = "(https?|ftp)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&;%$-]+)*@)*((?:(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|[1-9])\.){3}(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|[1-9]|0)|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}))(:\d+)*(($|[a-zA-Z0-9.,?'+\\&;%\$#=~_-]+))*";

Escape all the / or use another regex delimiter, i.e. ~ or #

if (preg_match("#$urlRegex#", $entry)) {

moreover, your regex could be shortened:

$urlRegex = "(https?|ftp)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&;%$-]+)*@)*((?:(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|[1-9])\.){3}(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|[1-9]|0)|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}))(:\d+)*(($|[a-zA-Z0-9.,?'+\\&;%\$#=~_-]+))*";
在梵高的星空下 2024-12-17 20:48:50

在正则表达式中使用主题标签,即:(

preg_match("#/\b$word1\b/i#", $entry)

您可能必须删除开头的斜杠)

Use hashtags in the Regex, ie:

preg_match("#/\b$word1\b/i#", $entry)

(You may have to remove that beginning slash)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文