Unknown modifier in preg_match() statement [duplicate]
Asked Answered
A

2

1

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/ )

Allargando answered 18/10, 2011 at 16:59 Comment(1)
See php.net/manual/en/regexp.reference.delimiters.php and possibly also preg_quote.Lashanda
C
1

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.,?'+\\&;%\$#=~_-]+))*";
Ciccia answered 18/10, 2011 at 17:50 Comment(0)
L
-1

Use hashtags in the Regex, ie:

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

(You may have to remove that beginning slash)

Large answered 18/10, 2011 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.