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

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

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(): Unknown modifier 'p' [duplicate]

This question already has answers here:
Unknown modifier '/' in ...? what is it? [duplicate]
(4 answers)
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 9 years ago.
Regex PHP Code
// If url matches regex
$regex = "/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/";
if (preg_match($regex, $this->value)) {
$this->valid();
}
Error Message
Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier 'p' in C:\Apache\www\profiletwist\lib\php\form\url.php on line 41
Call Stack
# Time Memory Function Location
1 0.0079 440016 {main}( ) ..\new.php:0
2 0.0964 667832 form->validate( ) ..\new.php:60
3 0.0968 668248 form_URL->validateUploadURL( ) ..\form.php:372
4 0.0969 668400 preg_match ( ) ..\url.php:41
Variables in local scope (#3)
$regex =
string '/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/' (length=79)
Question
How do I fix the regex for this "unknown modifier" error to not occur?
ultimately, I would like a regex that makes sure the text input matches:
"/upload/temporary/####_##_##_[A-z0-9 _-]+ "." [a-z]{3}
This is a filename target. The beginning does not change and the last part can be a random hash followed by an arbitrary extension. Further processing is done after the regex but this is the first test.
Thank you!
In a regex string you have to escape your delimiters. Or better: use a character which doesn't appear in the regex itself as delimiter:
other delimiter (recommended):
$regex = "#^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$#";
escaped delimiters:
$regex = "/^(\/upload\/temporary\/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ \/-]+.[A-z]{2,4}$/";
You need to escape the front slashes or just use another delimiter (I've used ! in this case):
$regex = "!^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$!"
$regex = "~^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[\w./-]+\.[a-z]{2,4}$~";
Change your delimiters to ~
When you use a delimiter for example /, you must escape all litteral / in your pattern otherwhise the regex engine believes that it is the end of the pattern.
Since u is a modifier and p isn't a modifier, you have this error because of the substring /^(/up....

Getting string from html tags with php [duplicate]

This question already has answers here:
Unknown modifier '/' in ...? what is it? [duplicate]
(4 answers)
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 9 years ago.
Regex PHP Code
// If url matches regex
$regex = "/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/";
if (preg_match($regex, $this->value)) {
$this->valid();
}
Error Message
Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier 'p' in C:\Apache\www\profiletwist\lib\php\form\url.php on line 41
Call Stack
# Time Memory Function Location
1 0.0079 440016 {main}( ) ..\new.php:0
2 0.0964 667832 form->validate( ) ..\new.php:60
3 0.0968 668248 form_URL->validateUploadURL( ) ..\form.php:372
4 0.0969 668400 preg_match ( ) ..\url.php:41
Variables in local scope (#3)
$regex =
string '/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/' (length=79)
Question
How do I fix the regex for this "unknown modifier" error to not occur?
ultimately, I would like a regex that makes sure the text input matches:
"/upload/temporary/####_##_##_[A-z0-9 _-]+ "." [a-z]{3}
This is a filename target. The beginning does not change and the last part can be a random hash followed by an arbitrary extension. Further processing is done after the regex but this is the first test.
Thank you!
In a regex string you have to escape your delimiters. Or better: use a character which doesn't appear in the regex itself as delimiter:
other delimiter (recommended):
$regex = "#^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$#";
escaped delimiters:
$regex = "/^(\/upload\/temporary\/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ \/-]+.[A-z]{2,4}$/";
You need to escape the front slashes or just use another delimiter (I've used ! in this case):
$regex = "!^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$!"
$regex = "~^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[\w./-]+\.[a-z]{2,4}$~";
Change your delimiters to ~
When you use a delimiter for example /, you must escape all litteral / in your pattern otherwhise the regex engine believes that it is the end of the pattern.
Since u is a modifier and p isn't a modifier, you have this error because of the substring /^(/up....

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 Regex "Unknown modifier p" [duplicate]

This question already has answers here:
Unknown modifier '/' in ...? what is it? [duplicate]
(4 answers)
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 9 years ago.
Regex PHP Code
// If url matches regex
$regex = "/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/";
if (preg_match($regex, $this->value)) {
$this->valid();
}
Error Message
Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier 'p' in C:\Apache\www\profiletwist\lib\php\form\url.php on line 41
Call Stack
# Time Memory Function Location
1 0.0079 440016 {main}( ) ..\new.php:0
2 0.0964 667832 form->validate( ) ..\new.php:60
3 0.0968 668248 form_URL->validateUploadURL( ) ..\form.php:372
4 0.0969 668400 preg_match ( ) ..\url.php:41
Variables in local scope (#3)
$regex =
string '/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/' (length=79)
Question
How do I fix the regex for this "unknown modifier" error to not occur?
ultimately, I would like a regex that makes sure the text input matches:
"/upload/temporary/####_##_##_[A-z0-9 _-]+ "." [a-z]{3}
This is a filename target. The beginning does not change and the last part can be a random hash followed by an arbitrary extension. Further processing is done after the regex but this is the first test.
Thank you!
In a regex string you have to escape your delimiters. Or better: use a character which doesn't appear in the regex itself as delimiter:
other delimiter (recommended):
$regex = "#^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$#";
escaped delimiters:
$regex = "/^(\/upload\/temporary\/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ \/-]+.[A-z]{2,4}$/";
You need to escape the front slashes or just use another delimiter (I've used ! in this case):
$regex = "!^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$!"
$regex = "~^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[\w./-]+\.[a-z]{2,4}$~";
Change your delimiters to ~
When you use a delimiter for example /, you must escape all litteral / in your pattern otherwhise the regex engine believes that it is the end of the pattern.
Since u is a modifier and p isn't a modifier, you have this error because of the substring /^(/up....

Categories