获得Last斜杠的价值。在PHP中

时间:2017-11-01 10:40:26

标签: php

我有如下的数组

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
)

我希望在最后一次斜线之后和之前获得价值,如SyR04A-94527,SyR05A-95528等。

2 个答案:

答案 0 :(得分:1)

你正在尝试解析文件名,PHP已经有很多内置函数来做到这一点。没有必要使用-ms-grid-column或正则表达式等操作字符串本身:

auto

答案 1 :(得分:0)

使用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]."\n");
}

输出:

 SyR04A-94527
 SyR05A-95528

您可以在此处http://sandbox.onlinephpfunctions.com/code/081543329dda8b9e0ef59836995184171ff4ce66

进行测试

我打算使用pathinfo,但我的沙盒网站已将其禁用,而且我懒得打开我的服务器。但它会是这样的:

$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)."\n";

干杯!