devjs11
devjs11

Reputation: 1958

php Preg_match pattern to solve

i have the following pattern that i am trying to solve with preg_match

  http://www.1.com/images/001/001/001/1.jpg
    http://www.2.com/images/002/002/002/2.jpg
    http://www.3.com/images/003/003/003/3.jpg
    http://www.4.com/images/004/004/004/4.jpg
    http://www.5.com/images/005/005/005/5.jpg
    etc.

i need to get only everything that goes after IMAGES and ends before last slash, for example 002/002/002

Hope i could explain well. Thank you.

Upvotes: 1

Views: 274

Answers (1)

Thai
Thai

Reputation: 11372

You have to use preg_match_all in this case.

<?php

$in = 'http://www.1.com/images/001/001/001/1.jpg
    http://www.2.com/images/002/002/002/2.jpg
    http://www.3.com/images/003/003/003/3.jpg
    http://www.4.com/images/004/004/004/4.jpg
    http://www.5.com/images/005/005/005/5.jpg';

if (preg_match_all('~images/(.*?)\.jpg~i', $in, $matches)) {

    print_r ($matches[1]);

} else {
    echo 'NOT FOUND';
}

Upvotes: 2

Related Questions