Using trim in an existing line of code

Hi, new here and just learning php (and javascript/jquey too a little).

I have some code which pulls a list of jpg files from a directory on the server and then displays them in a drop-down list using a hover event. It is only static at the moment and I have to refresh the page to update the list as new files are added but I am working on this being dynamic and doing it without a page refresh (but that’s another story).
For now I just need help with the display of the files. I don’t want it to display the extension. The file names themselves will never conatin a period or letters j, p or g in them so I figured I could use trim to remove them characters.

this is my php code at the moment. As such it displays the full file name including extension:

  $files = array_slice(scandir('upload/'), 2);
  foreach ($files as $file)
     echo "<a href='upload/$file'>$file</a>";

Somehow I need to get the following code integrated into the last line:

trim($file, '.jpg');

This is where I am in need of help.
It would look a bit like the below I think (I just need to get the syntax correct).

echo "<a href='upload/$file'>trim($file, '.jpg');</a>";

Can anyone please help?

Many thanks.

OK the code does not look quite like the above! I need to figure out how I insert code correctly into a post first. Sorry.

<edit>OK think I sorted it now</edit>

php’s function pathinfo would be cleaner to get the filename without the extension. (see the first example)

foreach ($files as $file) {
    $file = pathinfo($file)['filename'];
     echo "<a href='upload/$file'>$file</a>";

Thanks for that. I actually have the dynamic version with jquery, php and ajax working now but your code works a treat too and will be useful for the future. Cheers :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service