duanjianqu3685 2015-05-19 11:18
浏览 27
已采纳

如何在PHP中删除文件名的开头?

I'm making a gallery in jssor and the slides are made by reading a directory and writing the div for every image using a PHP script. Under the image a caption is displayed with the name of the artist (this is always the same) followed by the file name without the extension.

This is what I have so far:

<?

$dir = 'Photo/Paintings';
$files = scandir($dir);
sort($files);
foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        echo '<div>
                <img u="image" style="max-height:460px;" src="Photo/Paintings/'.$file.'" />
               <img u="thumb" src="Photo/Paintings/'.$file.'" />';
        $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
            echo '<div u="caption" style="position: absolute; top: 470px; left: 0px; height: 10px; text-align: center;">
                      ARTISTX - '.$withoutExt.'
              </div>
        </div>';
    }
}

?> 

These images should be in a particular order, so my idea was to add a number at the beginning of every filename like so #5-Picture of tree.jpg. My question is, how do I remove this #5 part from being displayed in the caption as well?

Alternatively, is there a better way to determine the order of these files? I realize now my idea wouldn't even work very well since #1 would alphabetically be followed by #11 and #12 instead of #2. I could maybe work around this by using a combination with letters 1A, 1B, 1C, 2A, etc.

  • 写回答

3条回答 默认 最新

  • douyuan3842 2015-05-19 11:23
    关注

    This only take effect if the file name actually starts with a number:

    $str = preg_replace('/^\\d+-/', null, $fileName);
    

    Files which do not start with #{number}- will not be affected by this tailoring process.

    For your sorting problem. Put a filling 0 before. So use 01, 02, ... and so on up to 99.

    By the way. Don't use short PHP tags. Always use <?php at the beginning of a PHP block.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化