Get value of Last slash and before . in php [duplicate] - php

This question already has answers here:
How can I get the name of the image from url?
(8 answers)
Closed 5 years ago.
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..

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

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

Related

PHP Sort Array Returned From FTP Server

I have this array returned from my home NAS FTP server:
$arr = array (
0 => 'FOLDER./Vacation.Paris.2018.5Month-Kodak[town]',
1 => './Vacation.Paris.2018.5Month-Kodak[town]/DESC.txt',
2 => './Vacation.Paris.2018.5Month-Kodak[town]/NUMBERS_CONTACT.vbs',
3 => './Vacation.Paris.2018.5Month-Kodak[town]/paris.vacation.2018.5month-kodak.mkv',
4 => './Vacation.Paris.2018.5Month-Kodak[town]/paris.vacation.2018.5month-kodak.dbs',
5 => 'FOLDER./Vacation.Dubai.2018-Kodak',
6 => './Vacation.Dubai.2018-Kodak/DESC.txt',
7 => './Vacation.Dubai.2018-Kodak/NUMBERS_CONTACT.vbs',
8 => 'FOLDER./Vacation.Dubai.2018-Kodak/Family',
9 => './Vacation.Dubai.2018-Kodak/Subs/2_IMG00536.jpg',
10 => './Vacation.Dubai.2018-Kodak/Subs/3_IMG00537.jpg',
11 => './Vacation.Paris.2018.5Month-Kodak[town]/Vacation.Dubai.2018.Center-Kodak.mp4',
12 => './Vacation.2019.5Month.Sweden-Kodak.mp4',
);
As you can see i have many folders of my vacation and now i im writing html/css/jquery photo album but i need to get first in php correct order above needs to output:
[0] => FOLDER./Vacation.Paris.2018.5Month-Kodak[town]
[1] => ./Vacation.Paris.2018.5Month-Kodak[town]/DESC.txt
[2] => ./Vacation.Paris.2018.5Month-Kodak[town]/NUMBERS_CONTACT.vbs
[3] => ./Vacation.Paris.2018.5Month-Kodak[town]/paris.vacation.2018.5month-kodak.mkv
[4] => ./Vacation.Paris.2018.5Month-Kodak[town]/paris.vacation.2018.5month-kodak.dbs
[5] => ./Vacation.Paris.2018.5Month-Kodak[town]/Vacation.Dubai.2018.Center-Kodak.mp4
[6] => FOLDER./Vacation.Dubai.2018-Kodak
[7] => ./Vacation.Dubai.2018-Kodak/DESC.txt
[8] => ./Vacation.Dubai.2018-Kodak/NUMBERS_CONTACT.vbs
[9] => FOLDER./Vacation.Dubai.2018-Kodak/Family
[10] => ./Vacation.Dubai.2018-Kodak/Subs/2_IMG00536.jpg
[11] => ./Vacation.Dubai.2018-Kodak/Subs/3_IMG00537.jpg
[12] => ./Vacation.2019.5Month.Sweden-Kodak.mp4
This is code that member from this forum try but it is not working correctly:
https://3v4l.org/208ol
So if you could help me modify above code so that i can get desired output.
Thanks
This uses usort to do the work and there is a bit of fiddling needed to get the sort order your after.
The first part is extracting the year from the string - it's assumed that it will be something like xxxxx 2020 xxxxx which is why it uses the second element from the regex used here. Then it will remove FOLDER only from the front of the string to ensure it isn't removed anywhere else. Then to get the order 'right', it uses the year followed by the processed string...
usort($arr, function($a, $b) {
$f = "FOLDER";
if ( substr( $a, 0, strlen($f)) == $f ) {
$a = substr( $a, strlen($f));
}
$y1 = preg_split("/\D+/", $a)[1];
if ( substr( $b, 0, strlen($f)) == $f ) {
$b = substr( $b, strlen($f));
}
$y2 = preg_split("/\D+/", $b)[1];
return $y1.$a <=> $y2.$b;
});
print_r($arr);
This uses the spaceship operator for the comparison, which is PHP 7+ only.

PHP preg_grep not working?

$fileNameMatchRegex = ^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$
$fileNames =
Array
(
[0] => index.html
[1] => a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv
[2] => a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv
[3] => a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv
[4] => a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv
[5] => a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv
[6] => a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv
[7] => a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv
[8] => a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv
[9] => a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv
)
$matchingFileNames = preg_grep ("/".$fileNameMatchRegex."/", $fileNames);
This works here...I get a match on 1-9: regex tester
Its working fine for me
$fileNames = explode(',', "index.html,a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv,a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv,a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv,a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv,a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv,a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv,a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv,a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv,a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv");
$fileNameMatchRegex = '^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$';
$matchingFileNames = preg_grep ("#".$fileNameMatchRegex."#", $fileNames);
print_r($fileNames);
print_r($matchingFileNames);
but i used '#' around the regexp as delimiter, as your regexp included '/',
you proberly just need to escape your '/' or chose a better delimiter

sort files in a directory based on partial file name in php

I am using glob() php function to find the files in a directory.
I am getting the following array as result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[6] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-5.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
)
What I want is "To sort the files on the basis of partial text of file names". As in the above array, when you may realize I was supposed to expect the result to be sorted as the ...-0.jpeg, ...-1.jpeg, ...-2.jpeg not ...-0.jpeg, ...-1.jpeg, ...-10.jpeg.
I am expecting the following result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
)
Kindly help, how I can do it using PHP with/without regular expression?
Use natsort function that sort an array using a "natural order" algorithm
As you have all strings in an array, simply use php's built-in function sort().
It would work like this:
sort($array);
Since all files are in the same directory, this should solve the incorrect order you have. If it doesn't, you can also try this:
//Get rid of directory
foreach($array as $key=>$value){
$value=str_replace('D:\xampp\htdocs\myproject\app\webroot\batch\temp\\','',$value); //Watch out, the last backslash has to be escaped with another backslash, otherwise the string will not end
}
//Sort only the filenames
$sort($array);
//Prepend directory again
foreach($array as $key=>$value){
$value='D:\xampp\htdocs\myproject\app\webroot\batch\temp\\'.$value;
}

preg_match or operator

My code below produces an error, unknown modified "|"... I'm trying to use it as the OR operator. What is the correct way to run this code without error?
$p = "(\w+)|(\()|(\))|(\,)";
$s = "sum(blue,paper,green(yellow,4,toast)))";
preg_match($p,$s, $matches);
print_r($matches);
Edit
Okay I changed it a bit... ~(\w+|\(|\)|,)~
Now... here's the problem: I need to take that string and split it into an array like this:
array("sum","(","blue","paper","green","(" ... etc );
Can someone help me do that? when I run the above expression it outputs an empty array....
Thanks
You are missing the delimiter for your pattern.
$p = "~(\w+)|(\()|(\))|(\,)~";
You're missing the delimiter as #Crayon correctly mentioned, also this pattern does the same thing:
$p = '~(\w+|[(]|[)]|,)~';
As for your (new) problem, try this:
$p = '~([(),])~';
$str = 'sum(blue,paper,green(yellow,4,toast)))';
$res = preg_split($p, $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($res);
Output:
Array
(
[0] => sum
[1] => (
[2] => blue
[3] => ,
[4] => paper
[5] => ,
[6] => green
[7] => (
[8] => yellow
[9] => ,
[10] => 4
[11] => ,
[12] => toast
[13] => )
[14] => )
[15] => )
)

php , SimpleXML, extract info from array

I'm trying to extract some information from ebay api . I have this link up http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=Morcovar-c74b-47c0-954f-463afb69a4b3&siteid=0&version=525&IncludeSelector=ItemSpecifics&ItemID=220617293997,250645537939,230485306218 and I'm using SimpleXML . So far I managed to extract all the info that I needed except the "PictureURL". The issue is that when I make a print to the simpleXMl it appears that the pictures are in an array
[GalleryURL] => http://thumbs4.ebaystatic.com/pict/1105372138158080_1.jpg
[PictureURL] => Array
(
[0] => http://i.ebayimg.com/09/!Bt+mKZQCWk~$(KGrHqYH-CYEvrwcUC47BL-,(K(pnw~~_1.JPG?set_id=8800005007
[1] => http://i.ebayimg.com/01/!Bt+mDTwB2k~$(KGrHqUH-DEEvirheBYUBL-,WtytJQ~~_1.JPG?set_id=8800005007
[2] => http://i.ebayimg.com/22/!Bt+mE8!EGk~$(KGrHqUH-CMEvsjKcE3JBL-,Wzr+sw~~_1.JPG?set_id=8800005007
[3] => http://i.ebayimg.com/17/!Bt+mFg!EGk~$(KGrHqIH-DoEvp43,)33BL-,W14vYQ~~_1.JPG?set_id=8800005007
[4] => http://i.ebayimg.com/01/!Bt+mQ0!!2k~$(KGrHqIH-EQEvqDDLQZVBL-,(j1YGg~~_1.JPG?set_id=8800005007
[5] => http://i.ebayimg.com/01/!Bt+mSq!EGk~$(KGrHqQH-C4Evs(Rz(JWBL-,(rdtsw~~_1.JPG?set_id=8800005007
[6] => http://i.ebayimg.com/03/!Bt+mUBw!Wk~$(KGrHqEH-DEEvnQtM9VkBL-,(w1+lQ~~_1.JPG?set_id=8800005007
[7] => http://i.ebayimg.com/15/!Bt+mHKQEGk~$(KGrHqQH-E!Evlr98iwBBL-,W87Nug~~_1.JPG?set_id=8800005007
[8] => http://i.ebayimg.com/13/!Bt+mI3!Bmk~$(KGrHqMH-DkEvq1,F2ooBL-,(EQ7Vg~~_1.JPG?set_id=8800005007
[9] => http://i.ebayimg.com/05/!Bt+mL2gEWk~$(KGrHqIH-EYEvov7vQY4BL-,(PzCKQ~~_1.JPG?set_id=8800005007
[10] => http://i.ebayimg.com/24/!Bt+mNlwEWk~$(KGrHqIH-CYEvqPjh6RQBL-,(Wh+S!~~_1.JPG?set_id=8800005007
[11] => http://i.ebayimg.com/19/!Bt+mPE!!2k~$(KGrHqQH-CYEvr5z9)NVBL-,(c3bzw~~_1.JPG?set_id=8800005007
)
But if I try to get them from the array I get no result . Here is the code that I currently have
$items = "220617293997,250645537939,230485306218,110537213815,180519294810";
$number_of_items = count(explode(",", $items));
$xml = $baseClass->getContent("http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=Morcovar-c74b-47c0-954f-463afb69a4b3&siteid=0&version=525&IncludeSelector=ItemSpecifics,description&ItemID=$items");
writeDoc($xml, "api.xml");
$getvalues = simplexml_load_file('api.xml');
$picture_url = $getvalue->Item[$number]->PictureURL[2];
echo "picture url is $picture_url";
Using the above code I was expecting to extract the 2nd picture url from the array .
thank you in advance for any help !
Can't test this, I'm afraid, but have you tried var_dump($picture_url) to see what $picture_url is? Maybe it needs to be cast to string?
echo "picture url is" . (string)$picture_url;

Categories