Regular expression for substring selection

I have a string (relative address) ../folder1/folder2/folder3
How to write a regular expression for the preg_match function to get the string /folder3,
or at least just folder3, i.e. last folder? Please help.


Answer 1, authority 100%

$filename is your string.

1)

basename($filename) //  ,    

2)

function getBasename($filename){ //  ,      
  $pos = strrpos($filename, '/');  
    if($pos===false) {  
      return false;  
  } else {  
    return substr($filename, $pos+1);  
  }  
}

Regulars here, IMHO, are redundant. 🙂


Answer 2, authority 100%

preg_match('#^.*/([^/]+)$#', $path, $match); // =)

Also

$folder = end(explode('/', $path));