Why preg_replace throws me a Unknown modifier error?
You are using forward-slashes as your regex pattern delimeter, so /<title>(.*)</title>/Ui'
will not work (</title>
has a forward slash).
You should be able to escape the forward slash or use a different delimiter that is not contained within the pattern, for example
'/<title>(.*)<\/title>/Ui' //(esacaping)
or
'~<title>(.*)</title>~Ui' //different delimiter
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()
)
Unknown modifier '/' preg_replace()
You are using your delemiter in the regexString (http://
).
Use another delemiter instead (e.g. #
).
The Explanation behind the error-message:
preg_replace
thinks that your string is terminated at the first slash of http://
. Since the modifiers follow after the delemiter which is /
in your case, it thinks, that the second slash has to be a modifier. There is no modifier /
- so it crashes
Why preg_replace throws me a Unknown modifier error?
You are using forward-slashes as your regex pattern delimeter, so /<title>(.*)</title>/Ui'
will not work (</title>
has a forward slash).
You should be able to escape the forward slash or use a different delimiter that is not contained within the pattern, for example
'/<title>(.*)<\/title>/Ui' //(esacaping)
or
'~<title>(.*)</title>~Ui' //different delimiter
I have a couple preg_replace unknown modifiers warnings on a string and I don't know the regex fix
You need to escape
/
in a regex expression. It should be\/
It should be
\s*
instead ofs*
to match zero or more white spaces.
Change:
(<brs*/?>s*){1,}
To:
(<br\s*\/?>\s*){1,}
Here is online demo where you can validate your regex expression as well.
PHP preg_replace unknown modifier
Simply change your Regex delimiter to something that's not used in the pattern, in this example I used @
which works fine.
preg_quote
only escapes . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
, so when using a non-escaped character in your pattern, but also as your regex delimiter, it's not going to work as expected. Either change the delimiter as above, or pass it into preg_quote
explicitely as part of the preg_quote($str, $delimiter)
overload.
$content = "{youtube}omg{/youtube}";
$find = array();
$replace = array();
$find[] = '{youtube((?!}).)*}';
$replace[] = '[embed]http://www.youtube.com/watch?v=';
$find[] = '{/youtube((?!}).)*}';
$replace[] = '[/embed]';
foreach ( $find as $key => $value ) {
$find[$key] = '@' . preg_quote($value) . '@';
}
echo preg_replace($find, $replace, $content);
Replacing eregi_replace with preg_replace
You've been trying to remove the wrong kind of slashes. You need to remove forward slashes (/
) instead of backslashes (\
) since /
marks the end of a regular expression.
Just escape the /
before url:
\\[url=([^\\[]*)\\]([^\\[]*)\\[\\/url\\]
Related Topics
Need Response Body of Http 500 with File_Get_Contents (Php)
How to Make Sure That Values from MySQL Keep Their Type in PHP
Modify an Existing PHP Function to Return a String
What Does a Leading Zero Do for a PHP Int
How to Increase My "Advanced" Knowledge of PHP Further? (Quickly)
How to Make Good Use of Multicore Cpus in Your PHP/MySQL Applications
How to Check If an Integer Is Within a Range of Numbers in PHP
How to Perform Multiple Guzzle Requests at the Same Time
Get the Index of a Certain Value in an Array in PHP
How to Fix 'Creating Default Object from Empty Value' Warning in PHP
Storing Time Information: Timezone Required
How to Do Url Rewriting in PHP
Get the Values of 2 HTML Input Tags Having the Same Name Using PHP
How to Detect Fake Users ( Crawlers ) and Curl
What's the Best Way to Create a Single-File Upload Form Using PHP