Anna Riekic
Anna Riekic

Reputation: 738

php find out 2nd match

I'm trying to grab a few images from a site but have got stumped due to different file extensions...

I have 2 matches in preg_match_all()

if (preg_match_all('~http://foo.com/foo_(.*?).(.*?)~i', $returned_content, $matches)) {

1st is img name and 2nd is img extension and would like to work out what each extension is so I can use it later in the code:

Update, full code:

       //cURL function here get_data()

 $returned_content = get_data('http://foo.com/page/2');

        if (preg_match_all('~http://foo.com/foo_(.*?)\.(.*?)~i', $returned_content, $matches)) {

   foreach ($matches[1] as $key) {

    $file = 'http://foo.com/foo_' . $key . 'correct extension';// Need to have correct extension here
        echo '<img src="' . $file . '" alt="" />';
           }
        }

Upvotes: 1

Views: 73

Answers (1)

Mike Mackintosh
Mike Mackintosh

Reputation: 14245

<?php
$returned_content = "sdfasdfsdfshttp://foo.com/foo_sdfsdfsdf.fdfdsf";
preg_match_all('~http\://foo\.com/foo_(.*?)\.(.*?)$~i', $returned_content, $matches);
print_r($matches);

Returns

Array
(
[0] => Array
    (
        [0] => http://foo.com/foo_sdfsdfsdf.fdfdsf
    )

[1] => Array
    (
        [0] => sdfsdfsdf
    )

[2] => Array
    (
        [0] => fdfdsf
    )

)

Unless : and . are in character blocks [], : and . are used as modifiers, they must be escaped.

Upvotes: 1

Related Questions