PDA

View Full Version : unknown modifier "?" error in regular expression in php



mayanktalwar1988
06-07-2010, 01:54 AM
(https?://)?(www.)?[0-9A-Za-z]+\.com/[0-9A-Za-z]+/\?paged=[0-9]+

i am getting this error in php


Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '?' in C:\xampp\htdocs\expression.php on line 19

i have test it on online testers it is giving correct results but when i ran it in preg match it gave above error

traq
06-07-2010, 02:35 AM
could you post your entire code? it may be a problem with how you're defining the pattern inside the function.

mayanktalwar1988
06-07-2010, 03:39 AM
here is the compelete code

<?php
$url = 'blahblah';
$contents = file_get_contents($url);
preg_match_all("(https?://)?(www.)?[0-9A-Za-z]+\.com/[0-9A-Za-z]+/\?paged=[0-9]+",$contents,$out);
$len=count($out[0]);
for($i=0;$i<$len;$i++)
{
echo $out[0][$i];
echo "\n";
}
?>

james438
06-07-2010, 04:19 AM
without looking at it all too closely I notice that // are not being escaped. I was thinking that this may be due to the type of delimiters you were using when I noticed there weren't any. Almost anything can be used as a delimiter, but it just so happens that / is the most common. at present your script assumes that ( is the delimiter and it sees your pattern as (https?://)? being the complete pattern with ? as a modifier, which in PCRE is not one of the listed modifiers.

If you are still having trouble could you post some of the data from $contents that is causing your pattern to fail? That is important. The code you posted is helpful too. We just need more data to work with.

mayanktalwar1988
06-07-2010, 04:28 AM
i escaped it but still not solved

i testing for these substrings in the page source

http://blahblah.com/downloads/?paged=2
or
blahblah.com/downloads/?paged=2
or
www.blahblah.com/downloads/?paged=2
or
https://blahblah.com/downloads/?paged=2
or
http://www.blahblah.com/downloads/?paged=2
or
https://www.blahblah.com/downloads/?paged=2

i tested the expression here http://www.regular-expressions.info/javascriptexample.html
it worked
there and perfectly matched only this
http://www.blahblah.com/downloads/?paged=2 ..this is the one of the links which i am trying to match it matched nothing else.

but when i run it with php code the script dont even run it says unknown modifier ?

james438
06-07-2010, 04:46 AM
That is because you are still not using any delimiters ;) Put in delimiters. May I suggest "/" as one? Take another look at my previous post. I expanded it slightly with a small example.

EDIT: After further thought I would like to suggest you escape your dot in the www. part of your pattern as well.

EDIT: ?./*?'" and I believe \ are the characters that are typically escaped when you want them recognized as literals or "as is". The last one "\" I am less sure about though.

EDIT: read this (http://us.php.net/manual/en/regexp.reference.delimiters.php) on delimiters.