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.
Related
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).
This question already has answers here:
Simple preg_replace returns null
(3 answers)
Closed 8 years ago.
You'll have to forgive me as I am really bad at regex.
So here is what I'm trying to do. I'm working on pulling Option Chains from Google Finance's API. The only problem is Google doesn't wrap the JSON keys in quotes like they should be. I found a method of using a replace string method with a regex but it is in R and I'm working with PHP.
The JSON I'm working with is from here
R:
gsub('([^,{:]+):', '"\1":', json)
PHP:
$pattern = '([^,{:]+):';
$replacement = '"\1":';
$json = preg_replace($pattern, $replacement, $quote);
I tried the PHP code seen above and I'm getting:
[18-Jan-2015 21:34:36 America/Denver] PHP Warning: preg_replace(): Unknown modifier ':' in /home1/oldpizza/public_html/austingregory/stocks/index.php on line 10
I'm betting it is the difference between PHP and R regex patterns but I'm not sure about the difference as I am still very new to regex in general. But according to regex101 it should work...
Not quite sure where to go from there. If you could help me out with the regex or help me figure out why it isn't working that would be great.
Thank you!
Delimiter is required in preg_replace:
$pattern = '~([^,{:]+):~';
$replacement = '"\1":';
$json = preg_replace($pattern, $replacement, $quote);
/, ~ and # are usually used as delimiter, but () pair can also be used as delimiter, and that is the reason why you got the warning above. It is interpreting the outermost pair of () as delimiter, and : at the end as modifier (flag).
This question already has answers here:
parse youtube video id using preg_match [duplicate]
(10 answers)
Closed 8 years ago.
I have the following regex which works fine in a regex editor but when I pull it together in PHP i am getting and Unknown modifier '(' error come up.
preg_replace("(\[LINK\])(\S*)(\[\/LINK])", "<a>href=\'$2\'>$2</a>", $xtext);
This is my first question on SO so I hope I have given enough information. From my research I believe I am missing delimiters but tried ~ at the start and the end of the search pattern and still does not seem to work.
try this
preg_replace("/(\[LINK\])(\S*)(\[\/LINK])/", "<a>href=\'$2\'>$2</a>", $xtext);
Note the delimiters
Just try with:
$input = 'foo [LINK]http://google.com[/LINK] bar';
$output = preg_replace('/\[LINK\](.*?)\[\/LINK\]/', '$1', $input);
Output:
string 'foo http://google.com bar' (length=57)
This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm not very knowledgeable with regular expresions, so I don't understand why I'm getting this error.
I'm using the following code to match all the emails in the string $str and saving them in the array $match:
preg_match_all(
"/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/",
$str,
$match
);
Apparently there's a problem in the regex (which I got from here) because I'm getting this error:
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier
'=' in C:\xampp\htdocs\project\Framework\Sanitizer.class.php on
line 38
Can someone tell me what's the problem?
Thanks
You have to escape the forward slash before that equals sign, e.g:
preg_match_all(
"/^[a-zA-Z0-9.!#$%&’*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/",
$str,
$match
);
"/" is the delimiter for the whole expression, so it must be escaped in the regex itself
You need to escape many of the characters in that string with a backslash.
Some of those you want to escape is: /?^{|} .
I would have written it like this:
preg_match_all(
"/^[a-zA-Z0-9.!#$%&’*+\/=\?\^_`\{\|\}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/",
$str,
$match
);
I find this site quite useful when it comes to matching e-mail adresses:
http://www.regular-expressions.info/email.html
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'