删除 PHP 中的文件扩展名,同时创建链接
我有这段代码,它获取目录中的文件名,然后创建到这些文件的链接。但是,链接中的文本末尾有文件扩展名。我想删除它,但同时保留文件的正确链接,即 HTML 链接需要扩展名才能保留在其上 - 像这样:
<a href="/documents/other/file.pdf">File</a>
这是我的脚本:
$linkdir="documents/other";
$dir=opendir("documents/other");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "index.php")
{
array_push($files, $file);
}
}
natcasesort($files);
$files=array_reverse($files);
foreach ($files as $file)
print "<li><a href='/$linkdir/$file' rel='external'>$file</a></li>";
这是我需要集成以删除文件的代码扩展:
$name = substr($file, 0, strrpos($file, '.'));
任何对此的帮助将不胜感激。
I have this code which is taking the name of files in a directory and then creating a link to those files. However, the text in the link has the file extension on the end. I want to remove this but at the same time keep the correct link to the file i.e. the HTML link needs the extension to remain on it - like this:
<a href="/documents/other/file.pdf">File</a>
So here is my script:
$linkdir="documents/other";
$dir=opendir("documents/other");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "index.php")
{
array_push($files, $file);
}
}
natcasesort($files);
$files=array_reverse($files);
foreach ($files as $file)
print "<li><a href='/$linkdir/$file' rel='external'>$file</a></li>";
Here is the code I need to integrate to remove the file extension:
$name = substr($file, 0, strrpos($file, '.'));
Any help with this will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的意思是你想要这个吗?
我在你的帖子中找不到任何其他问题:)
Could you mean that you want this?
I can't find any other question in your post :)
使用
pathinfo()
是一个更强大的解决方案,以防某些文件的名称带有更多点(例如someclass.inc.php
):Using
pathinfo()
is a more robust solution, in case some files have name with more dots (likesomeclass.inc.php
):