php找出第二场比赛

时间:2012-06-14 19:00:57

标签: php preg-match-all

我正试图从网站上抓取一些图片但由于文件扩展名不同而感到难过......

我在preg_match_all()

中有2个匹配项
if (preg_match_all('~http://foo.com/foo_(.*?).(.*?)~i', $returned_content, $matches)) {

第一个是img名称,第二个是img扩展名,想知道每个扩展名是什么,所以我可以在以后的代码中使用它:

更新,完整代码:

       //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="" />';
           }
        }

1 个答案:

答案 0 :(得分:1)

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

返回

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

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

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

)

除非:和。在字符块[],:和。用作修饰符,必须将它们转义。