PHP preg_replace error: Unknown modifier ']' [duplicate] - php

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
Apologies for yet another preg_replace question!
I'm getting the error:
Warning: preg_replace() [function.preg-replace]: Unknown modifier ']' in C:\xampp\htdocs\reg.php on line 6
When running the following preg_replace:
$newContent = preg_replace('<img [^>]*.gif[^>]*?>','MATCHED',$newContent);
I've run the regex through a couple of online regex builders and it seems sound. Any ideas what I'm missing here? Am I using ] incorrectly?

You need a delimiter around your expression:
$newContent = preg_replace('|<img [^>]*.gif[^>]*?>|','MATCHED',$newContent);

Related

PHP- Warning: preg_match(): Unknown modifier '(' [duplicate]

This question already has answers here:
Unknown modifier '(' when using preg_match() with a REGEX expression [duplicate]
(5 answers)
Closed 4 years ago.
I am checking browser agent and version using preg match. but i found error while i update my php version 5.3 to 5.4
preg_match( '/Mozilla/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version)
have any idea how to fix this issue?
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.

Warning: preg_match_all(): Unknown modifier '4' [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I have the following error:
Warning: preg_match_all(): Unknown modifier '4' in file.php on line 410
Code on line 410:
preg_match_all("#$replacement_pattern#is", $text, $arr);
Can someone please help me to fix this problem?
The issue is caused by the pattern having an unescaped regex delimiter that is # in your case.
At line 396, the $row['bbcode_in'] is a simple string, and all # symbols can be escaped then, so that afterwards you could use # as regex delimiters.
So, Line 396 must look like:
$replacement_pattern = str_replace('#', '\\#', trim($row['bbcode_in']));

PHP Preg_match() Unknown Modifier Url [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I got this error message:
Warning: preg_match(): Unknown modifier '/' in C:\xampp\htdocs\easyblog\wp-content\themes\easyblog\inc\widget\flickr.php on line 225
Source code in here :
// Screen name or RSS in $username?
if (!preg_match("/http://api.flickr.com/services/feeds/", $username))
$url = "http://api.flickr.com/services/feeds/photos_public.gne?id=".urlencode($user_id)."&format=".$flickrformat."&lang=en-us".$tags;
else
$url = $username."&format=".$flickrformat.$tags;
Can someone help me, please!
You need to escape the forward slashes in your expression:
"/http:\/\/api.flickr.com\/services\/feeds/"
When you use / delimiter, then you must escape all / character if used inside pattern, for avoiding this you can use another delimiter such as #
"#http://api.flickr.com/services/feeds#"
However, your pattern needs escaping:
"/http:\/\/api.flickr.com\/services\/feeds/"

What's wrong with my code preg_match? [duplicate]

This question already has answers here:
PHP regular expressions: No ending delimiter '^' found in
(2 answers)
Closed 8 years ago.
While running this regex:
if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){`
I get the following error message:
Warning: preg_match(): No ending delimiter '^' found in C:\xampp\htdocs\Test\source\index.php on line 28.
Why?
preg_match() thinks you're using the carat "^" as the delimiter. Generally, you'll find most people use "/".
if(!preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/", $email)) {

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'd' [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I have a function that once in a while gives out an error:
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'd'
Most often it does not. I don't understand the pattern when it does and when it doesn't.
I know almost nothing about regex, so any help would be greatly appreciated.
Here's the function:
function textHighlight($haystack,$needle,$clr='yellow') {
$haystack=preg_replace("/($needle)/i","<span style='background:$clr;'>\${1}</span>",$haystack);
return $haystack;
}
Thank you.
You may have a '/' character in the $needle variable. You can replace /../ with #...#

Categories