Hello I’m trying to get php to return a filename and return the string as part of a rel tag that will be used by a javascript plugin.
I need to add some php script to return the filename string up to the second hyphon:
so far i have
<?php
$filename = basename ( get_attached_file( get_post_thumbnail_id() ) );
?>
<?php echo pathinfo($filename, PATHINFO_FILENAME); ?>
it returns
tulip-367-011 the filename is tulip-367-011.jpg how do I get it to return tulip-367
Hi I tried
<?php
$filename = basename ( get_attached_file( get_post_thumbnail_id() ) );
?>
<?php echo $file_parts = explode('-', basename($filename));
array_pop($file_parts); // Remove the last segment
$split_name = implode('-', $file_parts);
?>
And it returned Array – Not evan a horay! That would have really made me chuckle, I’ve been stumped on this for week now. Thanks for the Help again I will keep trying. I’m not a very good programmer yet so please feel free to laugh at my stupidity. Asa
And I tried
$file_parts = explode('-', basename($filename));
array_pop($file_parts); // Remove the last segment
$split_name = implode('-', $file_parts);
And it also said Array. Thanks again for your help.
Just use
basenameinstead offileinfo.Example:
Edit
Sorry I misread the original question, assuming the file names are consistent, to remove the last segment do something like this: