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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)