OmniUnlimited
asked on
Help with preg_match()
Hello Experts!
Can anyone tell me how I could set up preg_match($pattern, $subject, $matches) to be able to capture only the digits between an underscore character ("_") and an image extension (".jpg" for example)? In other words, let's say I have an image labeled "img_20131009_12.jpg". How would I be able to make it so that $matches[0] contains "12"?
Can anyone tell me how I could set up preg_match($pattern, $subject, $matches) to be able to capture only the digits between an underscore character ("_") and an image extension (".jpg" for example)? In other words, let's say I have an image labeled "img_20131009_12.jpg". How would I be able to make it so that $matches[0] contains "12"?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Outstanding gentlemen! The perfect answers in record time!
@Ray: As always, you astound me with your intimate knowledge of PHP. Thank you for your more efficient solution. But, since the question asked for help with preg_match, I'm afraid it is only fair to award mvidas the lion's share of the points for correctly answering the question.
@mvidas: Works like a charm. Thanks!
@Ray: As always, you astound me with your intimate knowledge of PHP. Thank you for your more efficient solution. But, since the question asked for help with preg_match, I'm afraid it is only fair to award mvidas the lion's share of the points for correctly answering the question.
@mvidas: Works like a charm. Thanks!
Glad to have helped! FYI, if you want to extract more data (like the date, extension, etc), you can surround that area with parenthesis to create another submatch.
If your pattern is /^img_(\d{8})_(\d+).([a-z0 -9])+$/i then your $matches would be
[0] img_20131009_12.jpg
[1] 20131009
[2] 12
[3] jpg
You can of course change that however needed if you want to further break it down.
Matt
If your pattern is /^img_(\d{8})_(\d+).([a-z0
[0] img_20131009_12.jpg
[1] 20131009
[2] 12
[3] jpg
You can of course change that however needed if you want to further break it down.
Matt
ASKER
Wow mvidas, that info is also very helpful!
Sorry I don't have any more points to award... :(
Sorry I don't have any more points to award... :(
Don't be sorry at all, just glad I could help :)
Open in new window