1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help using Regex to grab content between slashes.

Discussion in 'PHP' started by samie, Jan 30, 2014.

  1. #1
    Would anyone have a code to do the following?
    SEMrush
    Say my string is a URL:
    domain.com/oranges/bananas/ccccKVj1w3q/test.jpg

    How would I use regex to grab anything starting with /cccc

    So the regex would output this:
    ccccKVj1w3q
     
    Solved! View solution.
    samie, Jan 30, 2014 IP
    SEMrush
  2. #2
    try this one

    
    $str = 'domain.com/oranges/bananas/ccccKVj1w3q/test.jpg';
    preg_match('|/(cccc.*?)/|',$str,$match);
    print_r($match);
    
    PHP:
     
    stephan2307, Jan 30, 2014 IP
    samie likes this.
  3. samie

    samie Member

    Messages:
    269
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #3
    Thank you so much for this! I'll test this out shorty here when I get on my PC :)
     
    samie, Jan 30, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Depending on what you're trying to do, you can do this completely without a regex, and also regardless of the (seemingly) random output before the basename (filename).
    
    $url = array_reverse(explode('/',pathinfo('domain.com/oranges/bananas/ccccKVj1w3q/test.jpg')['dirname']));
    echo $url[0];
    
    PHP:
     
    PoPSiCLe, Jan 30, 2014 IP
  5. samie

    samie Member

    Messages:
    269
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #5
    I don't understand how that script is going to pinpoint the section of the slashes with the cccc. But I tried it anyways and it seemed to give an unexpected [ on line two where it says ['dirname'].

    I tried this and it works perfectly for what I need. Thanks again! :)
     
    samie, Jan 30, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    My example uses pathinfo (a built-in PHP-function, but it depends on which version of PHP you're running) to return information about the path given - dirname is one of the return-items in the array, and gives 'domain.com/oranges/bananas/ccccKVj1w3q' in the example above. It then takes that string, and using '/' to explode it, creates an array of each item - since you want the last item in the array, we use array_reverse, and pick out [0] - the first item in the new array.
    It works just fine, since I tested it before posting.
    pathinfo should run fine on PHP-version above 4.0.3, so... if it's not working, I would take a look at which PHP-version you're running
     
    PoPSiCLe, Jan 31, 2014 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    The parse error comes likely from ...j1w3q/test.jpg')['dirname']. Array dereferencing was added in PHP 5.5.

    You could fix this by suppling PATHINFO_DIRNAME to pathinfo().

    (Untested)
    
    $url = array_reverse(explode('/',pathinfo('domain.com/oranges/bananas/ccccKVj1w3q/test.jpg', PATHINFO_DIRNAME)));
    
    PHP:
     
    nico_swd, Jan 31, 2014 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    That's a bit special, then, since I tested it on PHP 5.4.3
     
    PoPSiCLe, Jan 31, 2014 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Ah yes, you're right. Function array dereferencing was added in 5.4. 5.5 adds string dereferencing, eg:

    
    echo 'this is a string'[3];
    
    PHP:
     
    nico_swd, Jan 31, 2014 IP