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>
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!
You're trying to parse filenames, and PHP already has plenty of built-in functions to do that. There's no need to manipulate the string itself with explode
or regular expressions, etc:
$filename = 'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg';
echo pathinfo($filename, PATHINFO_FILENAME);
// SyR04A-94527