Forum Moderators: coopster

Message Too Old, No Replies

Remove Extension or Last 4 characters

         

almo136

12:28 am on Mar 8, 2009 (gmt 0)

10+ Year Member



Hi

I'm using this code which outputs the total number of files in the specified directory and provides a link to each one.

<?php
$directory = opendir("../directory/");
while($item = readdir($directory)){
if(($item != ".") && ($item != "..")){
$files[] = $item;
echo "<a href=../directory/$dir$item>$item</a><br />";
}
}
$sizeofarray = count($files);
echo "$sizeofarray";
?>

I'd like to modify it so that it doesn't display the file extension in the links?

All of the files will be .php so it would also work if I could not display the last 4 characters.

Does anyone know how to achieve this?

Thanks!

IanKelley

12:39 am on Mar 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remove the last 4 characters:

$item = substr($item,0,-4);

Remove .php from the end of string:

$item = preg_replace('/\.php$/','',$item);

Remove the last period and everything following it from string:

$item = preg_replace('/\..*$/','',$item);

almo136

12:37 pm on Mar 8, 2009 (gmt 0)

10+ Year Member



thanks!

g1smd

11:48 pm on Mar 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I guess that you'll also need to add a rewrite to connect the extensionless URL requests with the correct local filenames.

eeek

10:58 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remove the last period and everything following it from string:

$item = preg_replace('/\..*$/','',$item);

No, that removes the first period and everything after it.

IanKelley

11:57 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're right, instead it should be:
$item = preg_replace('/\.[^.]+$/','',$item);

[edit: added missing /code]

[edited by: IanKelley at 11:59 pm (utc) on Mar. 16, 2009]