duanjianqu3685 2015-05-19 03: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 03: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部