Results 1 to 6 of 6

Thread: unknown modifier "?" error in regular expression in php

  1. #1
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default unknown modifier "?" error in regular expression in php

    PHP Code:
    (https?://)?(www.)?[0-9A-Za-z]+\.com/[0-9A-Za-z]+/\?paged=[0-9]+ 
    i am getting this error in php

    PHP Code:
    Warningpreg_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

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    could you post your entire code? it may be a problem with how you're defining the pattern inside the function.

  3. #3
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    here is the compelete code
    PHP 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";
    }
    ?>

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,285
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    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.
    Last edited by james438; 06-07-2010 at 09:45 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Sep 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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/...ptexample.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 ?

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,285
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    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 on delimiters.
    Last edited by james438; 06-07-2010 at 08:02 AM. Reason: grammar
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •