Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract filename from path [duplicate]

Tags:

regex

php

i need to get filename from file path string. For example, from this string \abc\def\filename.txt i need to get filename.txt

trying to do this with regexp:

$filepath="abc\filename.txt";
$filename = preg_replace("/.+\\/","",$filepath);

but it gives me an error. What regex i should use to solve this?

like image 761
nukl Avatar asked Jul 19 '10 02:07

nukl


2 Answers

you should use the function basename instead:

$filepath = 'abc\filename.txt';
$filename = basename($filepath);

edit: important note, you need to use single quotes when you have backslashes in your strings, else escape them properly.

note: this will not work:

$filepath = "abc\filename.txt";
$filename = basename($filepath);

because you're variable $filepath infact holds:

abc[special char here equalling \f]ilename.txt

another edit: this regex works too..

$filepath = '\def\abc\filename.txt';
$basename = preg_replace('/^.+\\\\/', '', $filepath);

all that was wrong with your original was that you had double-quotes rather than single, and backslash needs double escaped (\\ rather than \).

like image 57
nathan Avatar answered Oct 01 '22 03:10

nathan


Two solutions:

  1. preg_match()
  2. str_replace() with pathinfo()

1: preg_match()

Ok, so the problem is that you are using backslashes. You have to make sure that you do not use double quotes for defining your filepath, since the backslash is interpreted as an escape sequence. Use single quotes.

Additionally, with a regex, it's much simple to get a filename by moving from the back of the path until you hit a backslash.... the trick is that a backslash is \\\\.. here's why

Finally, you don't want to use preg_replace. Just find the filename with preg_match:

<?php
  // Use single quotes or the backslash will be interpreted as an esacpe sequence
$filepath = '\abc\def\filename.txt';

  // You have to use 4 backslashes to represent your single backslash 
  // The regex picks the characters that are NOT \ from the end of the path
$pattern = '/[^\\\\]+$/';

  // Use $matches to store the match
preg_match($pattern, $filepath, $matches);

  // Display answer now, or use later
echo $matches[0];
?>

2: str_replace() with pathinfo()

As others said, basename() is a good option. Another option, if there's any chance that you may also need the directory or other path information later down the line is to use pathinfo()

The problem is that both basename and pathinfo assume forward slashes, so you must convert your backslashes to forward slashes:

Example:

<?php
  // Make sure to use single quotes
$filepath='abc\filename.txt';
  // Replace backslash with forward slash
$filepath = str_replace('\\', '/', $filepath);
$path_parts = pathinfo($filepath);

  // This is the answer you want
echo $path_parts['basename'], "\n";

  // But you also have access to these
echo $path_parts['dirname'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
like image 41
Peter Ajtai Avatar answered Oct 01 '22 01:10

Peter Ajtai