1

Possible Duplicate:
How to get file name from full path with PHP?

echo '<td width="11%" class="imagetd">'. ( ( empty ($arrImageFile[$key]) ) ? "&nbsp;" : htmlspecialchars( is_array( $arrImageFile[$key] ) ? implode(",", $arrImageFile[$key]) : $arrImageFile[$key] ) ). '</td>' . PHP_EOL;
echo '<td width="11%" class="videotd">'. ( ( empty ($arrVideoFile[$key]) ) ? "&nbsp;" : htmlspecialchars( is_array( $arrVideoFile[$key] ) ? implode(",", $arrVideoFile[$key]) : $arrVideoFile[$key] ) ). '</td>' . PHP_EOL;
echo '<td width="11%" class="audiotd">'. ( ( empty ($arrAudioFile[$key]) ) ? "&nbsp;" : htmlspecialchars( is_array( $arrAudioFile[$key] ) ? implode(",", $arrAudioFile[$key]) : $arrAudioFile[$key] ) ). '</td>' . PHP_EOL;

In the above code I have 2 table columns which depending if there are content or not display information in the table cells. Now as you can see the three columns are Image, Video and Audio. Now these columns display the relevant file names in their table cells. Below is how they are displayed:

  • All Image Files contain the string ImageFiles/ before the filename, e.g. ImageFiles/Lighthouse.png
  • All Video Files contain the string VideoFiles/ before the filename, e.g. VideoFiles/sample_mpeg.mp4
  • All Audio Files contain the string AudioFiles/ before the filename, e.g. AudioFiles/Music.mp3

As you can see each file contains the directory string before the filename. But what I want to do is that for each column and for each file, remove the relative directory string or in other words remove ImageFiles/, AudioFiles/ and VideoFiles/ from the whole string of each filename as I do not want the user to know the name of the directory. But how can this be done?

Community
  • 1
  • 1
user1881090
  • 739
  • 4
  • 16
  • 34

3 Answers3

6

PHP has a function for it: basename

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
5

Use basename()

echo basename($arrImageFile[$key]);
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

Search for the last index of / and do a substring to extract everything after it.

Patashu
  • 21,443
  • 3
  • 45
  • 53