1

I would like to get substring:

myPhotoName

from the below string:

path/myPhotoName_20170818_111453.jpg

using PHP preg_match function.

Please may somebody help me?

Thank you.

3
  • 1
    What is your criteria and what did you try? Commented Aug 18, 2017 at 5:16
  • 1
    you can use preg_match('/myPhotoName/', $str); but I think this is not the case. I don't know what are you trying to do. Commented Aug 18, 2017 at 5:18
  • What have you tried this far? Where are you stuck? Please show some effort by yourself. See How to Ask and minimal reproducible example. Commented Aug 18, 2017 at 5:26

3 Answers 3

1

Preg_match from / to _.

$str = "blur/blur/myPhotoName_20170818_111453.jpg";
Preg_match("/.*\/(.*?)_.*(\..*)/", $str, $match);

Echo $match[1] . $match[2];

I use .*? to match anything between the slash and underscore lazy to make sure it doesn't match all the way to the last underscore.
Edited to make greedy match anything before the /

https://3v4l.org/9oTuj

Performance of regex:
enter image description here


Since it's such a simple pattern you can also use normal substr with strrpos.
Edited to use strrpos to get the last /

$str = "blur/blur/myPhotoName_20170818_111453.jpg";
$pos = strrpos($str, "/")+1; // position of /
$str = substr($str, $pos, strpos($str, "_")-$pos) . substr($str, strpos($str, "."));
// ^^ substring from pos to underscore and add extension

Echo $str;

https://3v4l.org/d411c

Performnce of substring:
enter image description here

My conclusion
Regex is not suitable for this type of job as it's way to slow for such a simple substring pattern.

Sign up to request clarification or add additional context in comments.

12 Comments

Will return incorrect result if the "photo name" contains an underscore.
But that is not what OP has said. If you want a flamewar then have a look at your own "pattern" it's extremely fixed. Any extra letter or char and it will fail.
lol I don't want a "flamewar". Chill. I'm just pointing it out. Though OP left no requirements so it's anyone's guess...
Then why point it out? It's quite obvious but you can say it yourself.
u are right for regex, which is not suitable for this type of simple problem.
|
1

Do like this:

<?php
$arr = "path/myPhotoName_20170818_111453.jpg";
$res = explode('_',explode('/',$arr)[1])[0];
print_r($res);
?>

Use explode function in place of preg_match for easily get your expected output. And using preg_match, do like this:

<?php
$img = "path/myPhotoName_20170818_111453.jpg";
preg_match("/path\/(.+)\_[0-9]*\_[0-9]*\.jpg/", $img, $folder);
print_r($folder[1]);
?>

1 Comment

It works brother. :) Thank you for the edit suggestion too.
0

This is what you need. Thanks to regex101.com you can now test all possible matches.

$content = "path/myPhotoName_20170818_111453.jpg";
preg_match('/path\/(.*?)_/', $content, $match);
echo $match[1];

#Output --> myPhotoName;

Match string will be in array which can be printed by using print_r($match);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.