-5

What is best practice to get 584ef6a14e69e out of string /files/028ou2p5g/blogs/70cw99r5k/584ef6a14e69e-120.jpg ?

Assume all data is not constant. I need symbols after last slash and before minus sign.

David
  • 4,332
  • 13
  • 54
  • 93
  • 3
    `parse_url()` could most likely do this as an alternate to regex. I.e.: http://stackoverflow.com/a/5870133/1415724 and http://stackoverflow.com/a/19644313/1415724 – Funk Forty Niner Dec 12 '16 at 19:43
  • What is consistent about the string? – chris85 Dec 12 '16 at 19:46
  • 1
    Seems like you already asked something similar http://stackoverflow.com/q/41036804/1415724 but the question wasn't marked as solved. This seems like a duplicate question to me. – Funk Forty Niner Dec 12 '16 at 19:49
  • and now someone popped an answer; so... comments? – Funk Forty Niner Dec 12 '16 at 19:50
  • Fred that topic didn't gave me the full answer I'll delete it and concentrate on this one. – David Dec 12 '16 at 20:00
  • You misphrased your intentions there like you did here. The question you asked there `I want to find FIRST such tag, and get image name from it 5844644f69fe7-64.jpg` was answered. Here's a hint though look at greedy and non-greedy regexs. Then be greedy until your `/` and then non-greedy till the `-`. – chris85 Dec 12 '16 at 20:04

3 Answers3

2

Use the proper tools so that there is no guesswork. This gets the filename without extension and then everything before the -:

$string = '/files/028ou2p5g/blogs/70cw99r5k/584ef6a14e69e-120.jpg';
$result = strstr(pathinfo($string, PATHINFO_FILENAME), '-', true);

Or slightly shorter:

$result = strstr(basename($string), '-', true);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Wow that's even better. But I've already gave solved solution to Andrew. sorry. Gave u plus! And many thanks! – David Dec 14 '16 at 06:02
  • Done :) by the way what's the best practice of getting extension from that string ? is there anything like basename() ? – David Dec 15 '16 at 05:22
0

Assuming it will always be -120.jpg and the part you want to get is composed of a-z and 0-9 characters, you may do as follow (using regex):

<?php

preg_match(
    '/([a-z0-9]+)-120\.jpg$/',
    '/files/028ou2p5g/blogs/70cw99r5k/584ef6a14e69e-120.jpg',
    $matches
);

var_dump($matches[1]);

otherwise, if always a number:

<?php

preg_match(
    '/([a-z0-9]+)-[0-9]+\.jpg$/',
    '/files/028ou2p5g/blogs/70cw99r5k/584ef6a14e69e-120.jpg',
    $matches
);

var_dump($matches[1]);

you may want to play more with regex in order to do something fitting more your needs (testing the length, case sensitivity, place in the string...)

Alexandre
  • 474
  • 2
  • 14
  • What about if everything is changeble ? I mean I want only symbols after last slash and before minus sign. – David Dec 12 '16 at 19:55
0
<?php

$string = '/files/028ou2p5g/blogs/70cw99r5k/584ef6a14e69e-120.jpg';
$string = basename($string);

// remove all characters after and including '-' if it exists in string
$pos = strpos($string, '-');
if ($pos !== false)
    $string = substr($string, 0, $pos);

echo $string; // outputs: 584ef6a14e69e

?>
Andrew Larsen
  • 1,257
  • 10
  • 21