duanshan188866 2017-11-01 10:40
浏览 16

获得Last斜杠的价值。 在PHP [重复]

This question already has an answer here:

I have array like below

Array
(
    [0] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg
    [1] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg
    [2] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR06A-961000001.jpg
    [3] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR06A-96529.jpg
    [4] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR07A-971000002.jpg
    [5] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR07A-97530.jpg
    [6] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR08A-981000003.jpg
    [7] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR08A-98531.jpg
    [8] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-991000004.jpg
    [9] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99532.jpg
    [10] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99533.jpg
    [11] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99534.jpg
    [12] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno53.jpg
    [13] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno54.jpg
    [14] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno55.jpg
    [15] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno56.jpg
    [16] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno57.jpg
)

I want to get value after last slash and before .jpg like SyR04A-94527,SyR05A-95528 etc..

</div>
  • 写回答

2条回答 默认 最新

  • douhuiyan2772 2017-11-01 10:51
    关注

    Using Regx:

    <?php
    $a = [
        'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg',
        'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg'
    ];
    
    foreach( $a AS $path ){
        if( preg_match('/\/([^\/]+)\.[a-z]+$/i', $path, $match))
            print_r($match[1]."
    ");
    }
    

    Outputs:

     SyR04A-94527
     SyR05A-95528
    

    You can test it here http://sandbox.onlinephpfunctions.com/code/081543329dda8b9e0ef59836995184171ff4ce66

    I was going to use pathinfo but my sandbox site has it disabled, and I'm to lazy to turn my server on. But it would be something like this:

    $a = [
       'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg',
       'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg'
    ];
    
    foreach( $a AS $path )
        echo pathinfo ($path, PATHINFO_FILENAME)."
    ";
    

    Cheers!

    评论

报告相同问题?

悬赏问题

  • ¥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 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?