Preg_match_all problem

Discussion in 'PHP' started by goscript, Nov 2, 2007.

  1. #1
    Hi,
    I am having the following function:
    function findinside($start, $end, $string) {
            preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $string, $m);
            return $m[1];
        }
    PHP:
    In the file i have:
    $start = "/product/";
    $end = ".html";
    $string="http://domaincom/product/product-name.html";
    PHP:
    When i call
    $out = findinside($start, $end, $string);
    PHP:
    It works with no problem and i get "product-name"

    But if $string contains ( or ) it stops working :
    $string="http://domaincom/product/product(best)-name.html";
    PHP:
    Any idea on fixing it?
    Thanks
     
    goscript, Nov 2, 2007 IP
  2. xemiterx

    xemiterx Peon

    Messages:
    62
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This might not help you with figuring out regular expressions, but in this case I don't believe you need to use one.

    PHP already has a function to return the file name:

    $string = 'http://domaincom/product/product(best)-name.html';
    echo basename($string); // product(best)-name.html
    echo basename($string, '.html'); // product(best)-name
    PHP:
     
    xemiterx, Nov 2, 2007 IP
    goscript likes this.
  3. goscript

    goscript Prominent Member

    Messages:
    2,753
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    315
    #3
    Yes but the problem is that i use preg_match_all as i have about 20 links/page that needs detected this way.
    So, in my case, $string can contain the source of an entire webpage.
     
    goscript, Nov 2, 2007 IP
  4. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #4
    
    function base_match_all($arr)
    {
       $all_your_base_are_belong_to_us = array();
       foreach($arr as $string)
       {
          $all_your_base_are_belong_to_us[] = basename($string, '.html');
       }
       return $all_your_base_are_belong_to_us;
    }
    
    $arr_string = base_match_all($strings);
    
    Code (markup):
    Maybe it is a bit slower, but it works just the same. :)

    Anyhow the preg_match_all problem might be because the ( ) needs to be back slashed and it is thinking (.*?) set type instead of exact string that needs to be found. So, it could be that preg_quote is not working like it should. hrm.. Think of changing ([^\.)]+) to something like (.*?) to match everything between?
     
    exodus, Nov 2, 2007 IP
  5. goscript

    goscript Prominent Member

    Messages:
    2,753
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    315
    #5
    Thanks exodus, will try to implement it to see what i get.
     
    goscript, Nov 2, 2007 IP