php preg_replace returns unknown modifier '+'? [duplicate] - php

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
trying to fix this regex. Its supposed to find any hyperlinks in a string and put anchor tags around them. Keeps coming back, unkown identifier '+'. I thought the plus sing was part of regex?
<?php
//replace links with clickable links
// match protocol://address/path/
$comments = preg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "\\0", $comments);
// match www.something
$comments = preg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1\\2", $comments);
?>
any help appreciated.

A PCRE patterns (that's what you give to preg_replace) needs to be enclosed by delimiters:
~[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*~
Here the ~ are the delimiters. I used this char because it doesn't occur in the rest of the regex.
To explain the error: PCRE thinks that [ is the delimiter (as the first char always is the delimiter). So when it find the corresponding closing delimiter ] is considers everything after it a modifier. And as there is no + modifier you get an error ;)

try replacing
"[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "\\0"
with
r'[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*', '\\0'

Related

PHP preg_match_all(): Unknown modifier '>' [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm trying to create a Regex to filter out HTML opening tags in PHP
So far I came up with this pattern /\<[^/>]*\>/.
This pattern seems to work on https://regexr.com/49vgk.
But as soon as I copy it into PHP I get this error:
PHP preg_match_all(): Unknown modifier '>'
PHP Code:
$input = '<p>This is my HTML text that I want <b>all</b> opening tags from</p>';
$regexPattern = '/\<[^/>]*\>/';
$openingTags = preg_match_all($regexPattern, $input);
So far I'm unable to figure out what is causing this issue. Mostly because I've escaped most characters.
Does someone in the StackOverflow community know what I'm doing wrong and if so could explain me what it is I'm doing wrong?
Thanks in advance.
First of all, using regex to parse HTML is evil.
Now that this is out of the way, here is a working script:
$input = '<p>This is my HTML text that I want <b>all</b> opening tags from</p>';
$regexPattern = '/<[^\/][^>]*>/';
preg_match_all($regexPattern, $input, $matches);
print_r($matches[0]);
Array
(
[0] => <p>
[1] => <b>
)
Here is an explanation of the pattern <[^\/][^>]*>:
< match an opening bracket
[^\/] match a single character other than /
[^>]* then match zero or more non closing bracket characters
> match a closing bracket
As for your current errors, you have defined / to be a delimiter for the regex pattern. This means that if you want to use a literal forward slash, you therefore must escape it (as you would a regex metacharacter).

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

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

php regex error, unknown modifier 'd' [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
Im trying to search for something on a page but i keep getting this silly error
this is the error i am getting
Warning: preg_match() [function.preg-match]: Unknown modifier 'd'
this is the code im using
$qa = file_get_contents($_GET['url']);
preg_match('/Click here/',$qa,$result);
And $_GET['url'] can eqaul many things but in one case it was http://freegamesforyourwebsite.com/game/18-wheeler--2.html
the the html of that url basically
Anyone got a clue :S ? I dont even know where to start cus i dont know what a modifire is and the php.net site is no help
thankyou !
You need to escape the '/' before download.php otherwise it thinks you are ending your regex and providing 'd' as a modifier for your regex. You will also need to escape the next '/' in the ending anchor tag.
preg_match('/<a href="\/download.php\?g=(?P<number>.+)">Click here<\/a>/',$qa,$result);
You have to escape your pattern delimiters or use different ones:
# v- escape the '/'
preg_match('/Click here/',$qa,$result);
# v- use hatch marks instead
preg_match('#Click here#',$qa,$result);
Your regular expression needs to be escaped correctly.
It should be:
'/<a href="\/download.php\?g=(?P<number>.+)">Click here<\/a>/'
The problem is that your regular expression is delimited by / characters, but also contains / characters as data. What it's complaining about is /download -- it thinks the / has ended your regular expression and the d that follows is a modifier for your regular expression. However, there is no such modifier d.
The easiest solution is to use some character that is not contained in the regex to delimit it. In this case, # would work well.
preg_match('#Click here#',$qa,$result);

Categories