regex - By using PHP preg_match, how to get specific substring from string
382
I would like to get substring:
myPhotoName
from the below string:
path/myPhotoName_20170818_111453.jpg
usingPHP
preg_match
function.
Please may somebody help me?
Thank you.
Answer
Solution:
Preg_match from / to _.
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:

Since it's such a simple pattern you can also use normal substr with strrpos.
Edited to use strrpos to get the last /
https://3v4l.org/d411c
Performnce of substring:

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