0

I have seen two similar questions on SO here & here but still stuck...

I have a string of filepath like catalog/reviews/249532-20691-5369.jpg and i want to get numbers only like 249532-20691-5369.

I am using the following approach:

$id1 = explode('/', $image['img_link']);
$id2 = explode('.', $id1[2]);
echo $id2;  //output 249532-20691-5369 

Is there any best approach to do this task in controller.

6
  • If the string is always in that form, this is a fine way of doing ths. If the string is ever different, regex may be a better option of the length of the numbers is always the same. Commented Dec 18, 2018 at 13:42
  • 1
    You could use a regex if you really want to, but if what you have works for you, why worry. Commented Dec 18, 2018 at 13:42
  • yes...always same lenght... Commented Dec 18, 2018 at 13:42
  • @JonStirling, I am not much fimilar with regex Commented Dec 18, 2018 at 13:44
  • @MuhammadHashirAnwaar Then stick with what you know? Commented Dec 18, 2018 at 13:44

4 Answers 4

7

https://ideone.com/XGwrTc

echo basename($image['img_link'], '.jpg');

an alternative if you have different file extensions:

echo pathinfo ( $image['img_link'] ,  PATHINFO_FILENAME );
Sign up to request clarification or add additional context in comments.

10 Comments

Beautifully simple!
But that still contains the .jpg file extension
@Philipp check updated
what if extension is dynamic.... means...some images will have .jpg others will have .png or else... then how can i handle that
@Alex , Accepted... (Y)
|
1

In your case, you could use a simple regular expression.

([^\/]+)\.jpg$

And in php this would look like this

preg_match('/([^\/]+)\.jpg$/', $image['img_link'], $m);
echo $m[1];

If you need it for any extension, you could use this

([^\/]+)\.[^\.]+$

So how does this work:

We start from the right, so we add an anchor to the line ending $. From there on, we want the extension, which is practically everything expect a point [^\.]+. Whats left is the file name, which is everything from the point until you reach the slash [^\/]+. This part is also enclosed in a capturing group () to access it in the end with $m[1].

2 Comments

Great, thanks! The only thing I can think of that would make your answer even better is a breakdown of how the regex works, but obviously that's up to you.
@GrumpyCrouton better?
0

explode returns an array, so you need to access the first element:

$ids = explode('/', $image['img_link'];
$id2 = explode('.', end($ids));

echo $id2[0];

Alternatively, you can just use basename() instead of the first explode() (assuming you always have .jpg extension):

echo basename($image['img_link'], '.jpg');

1 Comment

if extension is dynamic then???
0

You could use Regex

This pattern would work:

(\d{6}-\d{5}-\d{4})

Breakdown:

  • \d means "Any Digit"
  • {#} means a count of # values, and of course the dashes between the numbers.
  • Wrap the whole thing with parenthesis to capture the whole result

Code:

$string = "catalog/reviews/249532-20691-5369.jpg";

preg_match('(\d{6}-\d{5}-\d{4})', $string, $result);
echo $result[0];

Output:

249532-20691-5369

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.