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"?
LVL 17
OmniUnlimitedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Use explode() instead of preg_match!  It's easier to understand and will get you a result faster!

<?php // RAY_temp_omniunlimited.php
error_reporting(E_ALL);

$str = 'img_20131009_12.jpg';

function findTwelve($s)
{
    $top = current(explode('.', $s));
    $end = end(explode('_', $top));
    return $end;
}

echo findtwelve($str); // PRINTS 12

Open in new window

0
mvidasCommented:
Ray is correct. However, if you want to use preg_match, you can use submatches and then reference $matches[1] instead
/_(\d+)\.jpg/i

Open in new window

In this case, $matches[0] returns the full match     _12.jpg
and $matches[1] returns     12

Matt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
It will depend on your test data, but this shows how you might do it with the regular expression.  Look in $mat[1] instead of [0].

<?php // RAY_temp_omniunlimited.php
error_reporting(E_ALL);

$str = 'img_20131009_12.jpg';

$rgx
= '#'         // REGEX DELIMITER
. 'img_'      // LITERAL STRING
. '\d{8}'     // EIGHT DIGITS
. '_'         // LITERAL STRING
. '('         // START CAPTURE GROUP
. '[^.]{1,3}' // ANYTHING BUT DOT, UP TO THREE
. ')'         // END GROUP
. '#'         // REGEX DELIMITER
;

preg_match($rgx, $str, $mat);
var_dump($mat);

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

OmniUnlimitedAuthor Commented:
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!
0
mvidasCommented:
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
0
OmniUnlimitedAuthor Commented:
Wow mvidas, that info is also very helpful!

Sorry I don't have any more points to award... :(
0
mvidasCommented:
Don't be sorry at all, just glad I could help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.