Unknown modifier '/' error in PHP [duplicate] - php

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
preg_replace('/http:///ftp:///', 'https://', $value);
http:// and ftp:// inside $value should be replaced with https://
This code gives error:
preg_replace() [function.preg-replace]: Unknown modifier '/'
What is a true regex for this task?

Try using a different delimiter, say #:
preg_replace('#http://|ftp://#', 'https://', $value);
or (less recommended) escape every occurrence of the delimiter in the regex:
preg_replace('/http:\/\/|ftp:\/\//', 'https://', $value);
Also you are searching for the pattern http:///ftp:// which really does not make much sense, may be you meant http://|ftp://.
You can make your regex shorter as:
preg_replace('#(?:http|ftp)#', 'https', $value);
Understanding the error: Unknown modifier '/'
In your regex '/http:///ftp:///', the first / is considered as starting delimiter and the / after the : is considered as the ending delimiter. Now we know we can provide modifier to the regex to alter its default behavior. Some such modifiers are:
i : to make the matching case
insensitive
m : multi-line searching
But what PHP sees after the closing delimiter is another / and tries to interpret it as a modifier but fails, resulting in the error.
preg_replace returns the altered string.
$value = 'http://foo.com';
$value = preg_replace('#http://|ftp://#', 'https://', $value);
// $value is now https://foo.com

Use another delimiter character, like '#', for instance.
preg_replace('#/http://|ftp://#', 'https://', $value);
And do not confuse / and |

preg_replace('!(http://|ftp://)!', 'https://', $value);
Long story: Regexp needs to be enclosed in delimiters, and those have to be unique inside the whole regexp. If you want to use /, then the remaining /-s inside the regexp need to be escaped. This however makes the syntax look a bit ugly. Luckily you can use any character as delimiter. ! works fine, as will other characters.
The rest of the regexp just lists two options, either of which will be replaced with the second parameter.

preg_replace('|http:\/\/ftp:\/\/', 'https://|', $value);

Use this instead:
preg_replace('-(http|ftp)://-', 'https://', $value);
Or
preg_replace('/(http|ftp):\/\//', 'https://', $value);

Related

preg_match is not working properly [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....

Wordpress Rewrite Rules Inspector plugin [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
preg_replace('/http:///ftp:///', 'https://', $value);
http:// and ftp:// inside $value should be replaced with https://
This code gives error:
preg_replace() [function.preg-replace]: Unknown modifier '/'
What is a true regex for this task?
Try using a different delimiter, say #:
preg_replace('#http://|ftp://#', 'https://', $value);
or (less recommended) escape every occurrence of the delimiter in the regex:
preg_replace('/http:\/\/|ftp:\/\//', 'https://', $value);
Also you are searching for the pattern http:///ftp:// which really does not make much sense, may be you meant http://|ftp://.
You can make your regex shorter as:
preg_replace('#(?:http|ftp)#', 'https', $value);
Understanding the error: Unknown modifier '/'
In your regex '/http:///ftp:///', the first / is considered as starting delimiter and the / after the : is considered as the ending delimiter. Now we know we can provide modifier to the regex to alter its default behavior. Some such modifiers are:
i : to make the matching case
insensitive
m : multi-line searching
But what PHP sees after the closing delimiter is another / and tries to interpret it as a modifier but fails, resulting in the error.
preg_replace returns the altered string.
$value = 'http://foo.com';
$value = preg_replace('#http://|ftp://#', 'https://', $value);
// $value is now https://foo.com
Use another delimiter character, like '#', for instance.
preg_replace('#/http://|ftp://#', 'https://', $value);
And do not confuse / and |
preg_replace('!(http://|ftp://)!', 'https://', $value);
Long story: Regexp needs to be enclosed in delimiters, and those have to be unique inside the whole regexp. If you want to use /, then the remaining /-s inside the regexp need to be escaped. This however makes the syntax look a bit ugly. Luckily you can use any character as delimiter. ! works fine, as will other characters.
The rest of the regexp just lists two options, either of which will be replaced with the second parameter.
preg_replace('|http:\/\/ftp:\/\/', 'https://|', $value);
Use this instead:
preg_replace('-(http|ftp)://-', 'https://', $value);
Or
preg_replace('/(http|ftp):\/\//', 'https://', $value);

Unknown modifier '(' when using preg_match() with a REGEX expression [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm trying to validate dates like DD/MM/YYYY with PHP using preg_match(). This is what my REGEX expression looks like:
$pattern = "/^([123]0|[012][1-9]|31)/(0[1-9]|1[012])/(19[0-9]{2}|2[0-9]{3})$/";
But using it with a correct value, I get this message:
preg_match(): Unknown modifier '('
Complete code:
$pattern = "/^([123]0|[012][1-9]|31)/(0[1-9]|1[012])/(19[0-9]{2}|2[0-9]{3})$/";
$date = "01/03/2011";
if(preg_match($pattern, $date)) return TRUE;
Thank you in advance
Escape the / characters inside the expression as \/.
$pattern = "/^([123]0|[012][1-9]|31)\/(0[1-9]|1[012])\/(19[0-9]{2}|2[0-9]{3})$/";
As other answers have noted, it looks better to use another delimiter that isn't used in the expression, such as ~ to avoid the 'leaning toothpicks' effect that makes it harder to read.
You use / as delimiter and also in your expression. Now
/^([123]0|[012][1-9]|31)/
Is the "complete" expression and all following is expected as modifier, also the (.
You can simply use another delimiter.
~^([123]0|[012][1-9]|31)/(0[1-9]|1[012])/(19[0-9]{2}|2[0-9]{3})$~
Or you escape all occurence of / within the expression, but I would prefer another delimiter ;) Its more readable.
your delimiter is /, but you are using it inside the pattern itself.
Use a different delimiter, or escape the /
You could escape the slashes / as suggested. It'll eventually lead to the Leaning Toothpick Syndome, though.
It's common to use different delimiters to keep your regular expression clean:
// Using "=" as delimiter
preg_match('=^ ... / ... $=', $input);
checek date using below function
function checkDateFormat($date)
{
//match the format of the date
if (preg_match ("/^([0-9]{2}) \/([0-9]{2})\/([0-9]{4})$/", $date, $parts))
{
//check weather the date is valid of not
if(checkdate($parts[2],$parts[1],$parts[3]))
return true;
else
return false;
}
else
return false;
}
echo checkDateFormat("29/02/2008"); //return true
echo checkDateFormat("29/02/2007"); //return false

simple question about preg_match() in php

i used bellow code to search and find if http is includes in $url address user enters
if (!preg_match("/http:///", $user_website)
but i got this error
Warning: preg_match() [function.preg-match]: Unknown modifier '/' in
i know its becuase of // of http but how work arround this !?
Instead of having to escape every / in URL regexes it's often useful to use preg_* alternative characters to mark the start/end of the pattern.
if (!preg_match("#http://#", $user_website)
The delimiter you are using / is found in the pattern as well. In such cases you can either escape the delimiter in the pattern:
if (!preg_match("/http:\/\//", $user_website)
or you can choose a different delimiter. This will keep the pattern clean and short:
if (!preg_match("#http://#", $user_website)
You can escape the slashes like the other answers mention, or alternatively you can use different delimiters, preferably characters you won't use in your regex:
preg_match('~http://~', ...)
preg_match('!http://!', ...)
And you don't really need regex for this. String matching should be enough:
if (strpos($user_website, 'http://') !== false) {
// do something
}
See: strpos()
Surely you must do
$parts = parse_url($my_url);
$parts['scheme'] will then contain the url scheme (might be 'http').
Escape / characters with \ characters.
You need to escape literal characters. Place a back-slash before your forward slashes.
http:// becomes http:\/\/
if (!preg_match("/http:\/\//", $user_website)

Categories