Help with preg_match()

OmniUnlimited
OmniUnlimited used Ask the Experts™
on
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"?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
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

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
Most Valuable Expert 2011
Top Expert 2016

Commented:
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

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

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!
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

Author

Commented:
Wow mvidas, that info is also very helpful!

Sorry I don't have any more points to award... :(
Don't be sorry at all, just glad I could help :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial