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
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}$#"
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
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()
)
PHP - preg_match and Unknown modifier error
The problem is the delimiter /
because you use it in your regexp again.
You have to escape it \/
or use another delimiter like @
:
if (preg_match("@([0-9]{2})[-/.]([0-9]{2})[-/.]([0-9]{4})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})@", $dateToTest, $tab) == false)
See the example #3 in the Docu. There is also a manual about delimiters.
When does preg_match(): Unknown modifier error occur?
If the first name or last name contains a /
, your regex will look something like:
/john/doe$/
To preg_match
, this looks like the regex is /john/
, with the trailing doe$/
being the modifiers. Those are of course invalid modifiers. You need to escape the regex delimiters (/
) inside the regex itself using preg_quote
.
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
PHP- Warning: preg_match(): Unknown modifier '('
Apply \
(backslash) before second /
(forwardslash) to escape it
preg_match( '/Mozilla\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version);
preg_match('/Netscape([0-9])\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version1);
Note:- Any /
in between starting and ending /
(delementer) in preg_match()
need to be escaped to make it run fine.
Related Topics
PHP Unable to Load PHP_Curl.Dll Extension
Invoke External Shell Script from PHP and Get Its Process Id
How to View/Open a Word Document in My Browser Using with PHP or HTML
How to Display Special Characters in PHP
How to Run a .Sh File from PHP
Bubble Sort Implementation in PHP
How to Determine a File's True Extension/Type Programmatically
How to Join Two Tables with Ssp.Class.Php
Remove Double Quote in JSON_Encode()
How to Save Webpage as a Image File Using PHP
Trying to Access Array Offset on Value of Type Bool
Cakephp Database Connection "Mysql" Is Missing, or Could Not Be Created
PHP Short Circuit Lazy Evaluation, Where Is It in the PHP.Net Manual
#1193 - Unknown System Variable 'Lc_Messages' When Trying to Login to PHPmyadmin
Laravel Validation: Exists with Additional Column Condition - Custom Validation Rule