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

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 #...#

Related

preg_match(): Unknown modifier '/' [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
When i give this regular expression for validating URL it shows error...
preg_match(): Unknown modifier '/' in C:\wamp64\www\php\Form
Validation\form.php on line 36
if(!preg_match("/%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?#|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu/",$website)){
$websiteerror="Invalid URL";
Since you used forward slash / as the delimiter, you must escape it inside the regex:
if (!preg_match("/%^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu/",$website)) {
$websiteerror = "Invalid URL";
}
Now the code runs for me, but I still get this warning:
PHP Warning: preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 106 in source_file.php on line 3
It seems PHP doesn't completely like the hex ranges you used in some of your character classes.

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']));

preg_match(): Unknown modifier php function [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
Function not working it's giving me error
Warning: preg_match(): Unknown modifier '_' in /home/u170751922/public_html/include/functions/main.php on line 325
Here is my code :
function verify_valid_email($emailtocheck)
{
$eregicheck = "^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+#([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}\$/i";
return preg_match($eregicheck, $emailtocheck);
}
You've forgotten delimiters:
$eregicheck = "/^([-!#\$%&'*+.\/0-9=?A-Z^_`a-z{|}~])+#([-!#\$%&'*+\/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}\$/i";
-and, also, note, that / as a delimiter must be escaped in regex itself

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

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

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

Categories