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

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

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

Php Regex issue - "Unknown modifier R" [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm trying to extract some text in between a Request XML tag (between and tags) using this Regex:
(?<=(<Request>)).*(?=(</Request>))
RegexBuddy shows me that it's fine, but preg_match returns this error:
"Unknown modifier R". Placing a backslash before the "/" makes it so nothing returns. Does anyone know what the problem is?
Code:
$parsedQuery = fopen("c:\\query", "r") ;
$parsed ="".
while (!feof($parsedQuery)) {
$parsed .= fgets($parsedQuery) ;}
$reg = "#(?<=(<Request>)).*(?=(</Request>))#";
$match = array();
preg_match($reg, $parsed, $match);
print_r($match);
Edit: I now noticed that the file opens with an unidentified character (binary value is 3F) after the opening of each tag (the "<" character). I assume php's fgets implementation does this for security measures, could this be the problem, and is there any way to surpass it?
The problem is that in PHP (and in a lot of langugages) a regexp is something like /pattern/modifier where modifier could be, for example, g (multiple match). If you want to use / in your regexp you have to escape them with a \ :
/(?<=(<Request>)).*(?=(<\/Request>))/
See http://www.php.net/manual/en/pcre.pattern.php for more information about patterns in PHP.

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 preg_replace returns unknown modifier '+'? [duplicate]

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'

Categories