Unknown modifier '/' error in PHP
Try using a different delimiter, say #
:
preg_replace('#http://|ftp://#', 'https://', $value);
or (less recommended) escape every occurrence of the delimiter in the regex:
preg_replace('/http:\/\/|ftp:\/\//', 'https://', $value);
Also you are searching for the pattern http:///ftp://
which really does not make much sense, may be you meant http://|ftp://
.
You can make your regex shorter as:
preg_replace('#(?:http|ftp)#', 'https', $value);
Understanding the error: Unknown modifier '/'
In your regex '/http:///ftp:///'
, the first /
is considered as starting delimiter and the /
after the :
is considered as the ending delimiter. Now we know we can provide modifier to the regex to alter its default behavior. Some such modifiers are:
i
: to make the matching case
insensitivem
: multi-line searching
But what PHP sees after the closing delimiter is another /
and tries to interpret it as a modifier but fails, resulting in the error.
preg_replace
returns the altered string.
$value = 'http://foo.com';
$value = preg_replace('#http://|ftp://#', 'https://', $value);
// $value is now https://foo.com
PHP | preg_match(): Unknown modifier '(' in C:\xampp\htdocs\Folder\index.php on line 38
You forgot one "\" before "/)" on your pattern :
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
So php is thinking that "/(" is a modifier : http://php.net/manual/sr/reference.pcre.pattern.modifiers.php
Warning: preg_match(): Unknown modifier ''
preg_match
arguments are in the wrong order. First should be the regular expression.
So change:
preg_match($source_html, $rule, $matches);
to:
preg_match($rule, $source_html, $matches);
preg_match error Unknown modifier '('
Escape /
in the regular expression.
"/^([1-9]|0[1-9]|[1,2][0-9]|3[0,1])\/([1-9]|1[0,1,2])\/\d{4} ([0-1][0-9]|[2][0-3])(:([0-5][0-9])){1,2}(:([0-5][0-9])){1,2}$/"
# ^ ^
Otherwise, /
is recognised as end of the regular expression.
Or use different delimiters:
"#^([1-9]|0[1-9]|[1,2][0-9]|3[0,1])/([1-9]|1[0,1,2])/\d{4} ([0-1][0-9]|[2][0-3])(:([0-5][0-9])){1,2}(:([0-5][0-9])){1,2}$#"
Warning: preg_replace(): Unknown modifier
Why the error occurs
In PHP, a regular expression needs to be enclosed within a pair of delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character; /
, #
, ~
are the most commonly used ones. Note that it is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, i.e. <pattern_goes_here>
, [pattern_goes_here]
etc. are all valid.
The "Unknown modifier X" error usually occurs in the following two cases:
When your regular expression is missing delimiters.
When you use the delimiter inside the pattern without escaping it.
In this case, the regular expression is <div[^>]*><ul[^>]*>
. The regex engine considers everything from <
to >
as the regex pattern, and everything afterwards as modifiers.
Regex: <div[^> ]*><ul[^>]*>
│ │ │ │
└──┬──┘ └────┬─────┘
pattern modifiers
]
here is an unknown modifier, because it appears after the closing >
delimiter. Which is why PHP throws that error.
Depending on the pattern, the unknown modifier complaint might as well have been about *
, +
, p
, /
or )
or almost any other letter/symbol. Only imsxeADSUXJu
are valid PCRE modifiers.
How to fix it
The fix is easy. Just wrap your regex pattern with any valid delimiters. In this case, you could chose ~ and get the following:
~<div[^>]*><ul[^>]*>~
│ │
│ └─ ending delimiter
└───────────────────── starting delimiter
If you're receiving this error despite having used a delimiter, it might be because the pattern itself contains unescaped occurrences of the said delimiter.
Or escape delimiters
/foo[^/]+bar/i
would certainly throw an error. So you can escape it using a \ backslash if it appears anywhere within the regex:
/foo[^\/]+bar/i
│ │ │
└──────┼─────┴─ actual delimiters
└─────── escaped slash(/) character
This is a tedious job if your regex pattern contains so many occurrences of the delimiter character.
The cleaner way, of course, would be to use a different delimiter altogether. Ideally a character that does not appear anywhere inside the regex pattern, say #
- #foo[^/]+bar#i
.
More reading:
- PHP regex delimiters
- http://www.regular-expressions.info/php.html
- How can I convert ereg expressions to preg in PHP? (missing delimiters)
- Unknown modifier '/' in …? what is it? (on using
preg_quote()
)
preg_match(); - Unknown modifier '+'
You need to use delimiters with regexes in PHP. You can use the often used /
, but PHP lets you use any matching characters, so @
and #
are popular.
Further Reading.
If you are interpolating variables inside your regex, be sure to pass the delimiter you chose as the second argument to preg_quote()
.
preg_replace(): Unknown modifier '\\'
preg_replace(): Unknown modifier
occurs when $test
contains a /
.
preg_quote( $test );
should be
preg_quote( $test, '/' );
/
is the PCRE delimiter used in your '/(^|\W)...(\W|$)/i'
expression.
PHP PCRE's can have any delimiter, so you have to tell preg_quote()
which delimiter is used.
http://php.net/manual/en/regexp.reference.delimiters.php
Related Topics
Insert Query Check If Record Exists - If Not, Insert It
What Does the B in Front of String Literals Do
Why PHP Iteration by Reference Returns a Duplicate Last Record
Laravel Migration Table Already Exists, But I Want to Add New Not the Older
How to Use Break or Continue Within for Loop in Twig Template
PHP Simple Foreach Loop with HTML
PHP Remove All Characters Before Specific String
Jquery $.Ajax Request of Datatype JSON Will Not Retrieve Data from PHP Script
Foursquare API for Venue User Image Error
How to Convert Iso8601 to Date Format in PHP
Using Variables in MySQL Update (Php/Mysql)
How to Create a Base64Encoded String from Image Resource
Get Coupon Data from Woocommerce Orders
Avoid Public Folder of Laravel and Open Directly the Root in Web Server