preg_match(): Unknown modifier 'G' in testscripts [duplicate] - php

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
foreach ($expected_lines as $expected_line) {
if (!preg_match("/$expected_line\s*/", $contents)) {
$match = false;
$unmatched = $expected_line;
break;
}
}
This is my code which give me error
PHPUNIT_FRAMEWORK_ERROR_WARNING
preg_match(): Unknown modifier 'G'
Let me know where i m gone wrong

It seems that the regular expression passed to preg_match is incorrect after actual value of $expected_line is used in "/$expected_line\s*/".
Everytime you use data you got from users in regular expressions, you need to properly escape it to avoid situations like that - when characters provided by user break the expression. Use preg_quote for that:
$escaped_expected_line = preg_quote($expected_line, '/');
if (!preg_match("/$escaped_expected_line\s*/", $contents)) {
...
}
Check here for more info: https://secure.php.net/manual/pl/function.preg-quote.php

Related

Unknown modifier in pregmatch [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
We recently transferred our hosting and all of the sudden errors came up to our preg_match scripts.
$lines = explode("\n",$email); //Email message by email forwarding
if(preg_match("/^(.*)/ GMT(.*)/ <reply#pipe.mydomain.com>:/",$lines[$i],$matches)){
$message .= str_replace($matches[1],"",$lines[$i]);
}
It returns unknown modifier 'G' after removing G, it shows unknown modifier 'M' error.
Also tried preg_match_all() still same error shows up.
EScape the middle forward slashes or use different php delimiters.
"~^(.*)/ GMT(.*)/ <reply#pipe.mydomain.com>:~"
Use \h+ to match one or more horizontal spaces.
"~^(.*)/\h+GMT(.*)/\h+<reply#pipe.mydomain.com>:~"

what causes Warning: preg_match(): Unknown modifier 'p' [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 7 years ago.
I am having trouble I could not fix this error. I am using this upload script to check the mime type.
Warning: preg_match(): Unknown modifier 'p'
Warning: preg_match(): Unknown modifier 'g'
Warning: preg_match(): Unknown modifier '('
if (preg_match('/^image/p?jpeg$/i', $_FILES['upload']['type']) or
preg_match('/^image/gif$/i', $_FILES['upload']['type']) or
preg_match('/^image/(x-)?png$/i', $_FILES['upload']['type']))
{
// Handle the file...
}
else
{
$error = 'Please submit a JPEG, GIF, or PNG image file.';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
exit();
}
Thank you in advance
Everything after second / (closing delimiter) is considered flags. Use another delimiter such as ~
~^image/p?jpeg$~i
Or to match the delimiter literal inside the pattern escape it using the backslash:
/^image\/p?jpeg$/i
Most comfortable to pick a delimiter, that you don't need inside the pattern > no worry for escaping. Frequently used delimiters are /,~,#,# or even bracket style delimiters such as ( pattern ).
side note
You could combine all three preg_match to a single one using alternation:
if(preg_match('~^image/(?:gif|p?jpeg|(?:x-)?png)$~i', $_FILES['upload']['type'])) { ... }
(?: starts a non-capture group. Good for testing: regex101.com

Using preg_match to Find URL [duplicate]

This question already has answers here:
Warning: preg_match() [function.preg-match]: Unknown modifier '/' [duplicate]
(2 answers)
Closed 8 years ago.
I've been trying to use preg_match to find a form with a certain action URL.
Here the code
if (!preg_match('/<form method="post"
action="https[\:]//www.amazon.com/gp/aw/si.html/ref=aw_c_co".*?<\/form>/is', $page, $form)) {
die('Failed to CHECKOUT form!');
But I get an error message:
Warning: preg_match(): Unknown modifier '/'
I dont understand whats wrong.
Thanks
You have to escape (\) the / since these are your regex delimiters! Maybe you want to use other delimiters?
/ -> \/
You can use the following code :
$str = '<form method="post" action="https[\:]//www.amazon.com/gp/aw/si.html/ref=aw_c_co".*?<\/form>';
$str = preg_replace('^.*<form.*?action="(.+?)".*$', '$1', $str);
It give you : https[\:]//www.amazon.com/gp/aw/si.html/ref=aw_c_co
Good luck

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

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