切片变量输出

时间:2018-01-19 07:26:20

标签: php html image output slice

变量$ myLIST给出以下输出。

<img id='badge-21582754-4' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_b9d6a65d9d9f70575971f26f3b302114.gif' alt='1' title='1' style='width:100px; height:100px; left:0px; top:0px'/>
<img id='badge-21582754-5' class='badgeimg' src='http://userimages03.website.com/userdata/21582754/badge_aa7fed804d36a11c149826e22138e3c5.gif' alt='2' title='2' style='width:100px; height:100px; left:100px; top:0px'/>
<img id='badge-21582754-6' class='badgeimg' src='http://userimages02.website.com/userdata/21582754/badge_52780efa210a1cbc468dc627a38c88d8.gif' alt='5' title='5' style='width:40px; height:100px; left:400px; top:0px'/>
<img id='badge-21582754-7' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_9c56d5678c031b775c6b3b24857d403b.gif' alt='4' title='4' style='width:100px; height:100px; left:300px; top:0px'/>
<img id='badge-21582754-8' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_da55f03e5ea241054773c2903d7920ed.gif' alt='3' title='3' style='width:100px; height:100px; left:200px; top:0px'/>

如何为每个输出提取IMC ID内的SRC链接,TITLES和8位数字?谢谢!

LE。谢谢大家,问题解决了!

3 个答案:

答案 0 :(得分:2)

您可以使用DOMDocument

$html = "<img id='badge-21582754-4' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_b9d6a65d9d9f70575971f26f3b302114.gif' alt='1' title='11111' style='width:100px; height:100px; left:0px; top:0px'/>
<img id='badge-21582754-5' class='badgeimg' src='http://userimages03.website.com/userdata/21582754/badge_aa7fed804d36a11c149826e22138e3c5.gif' alt='2' title='2' style='width:100px; height:100px; left:100px; top:0px'/>
<img id='badge-21582754-6' class='badgeimg' src='http://userimages02.website.com/userdata/21582754/badge_52780efa210a1cbc468dc627a38c88d8.gif' alt='5' title='5' style='width:40px; height:100px; left:400px; top:0px'/>
<img id='badge-21582754-7' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_9c56d5678c031b775c6b3b24857d403b.gif' alt='4' title='4' style='width:100px; height:100px; left:300px; top:0px'/>
<img id='badge-21582754-8' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_da55f03e5ea241054773c2903d7920ed.gif' alt='3' title='3' style='width:100px; height:100px; left:200px; top:0px'/>";
$doc = new DOMDocument();
$doc->loadHTML($html);
$imageTags = $doc->getElementsByTagName('img');
$template_array= array();
foreach($imageTags as $key=>$tag)
{ 
    $template_array[$key] = array('src'=>$tag->getAttribute('src'),'title'=>$tag->getAttribute('title') , 'id'=> explode("-",$tag->getAttribute('id'))[1]);
}
echo "<pre>";print_r($template_array);

输出: -

Array
(
    [0] => Array
        (
            [src] => http://userimages01.website.com/userdata/21582754/badge_b9d6a65d9d9f70575971f26f3b302114.gif
            [title] => 11111
            [id] => 21582754
        )

    [1] => Array
        (
            [src] => http://userimages03.website.com/userdata/21582754/badge_aa7fed804d36a11c149826e22138e3c5.gif
            [title] => 2
            [id] => 21582754
        )

    [2] => Array
        (
            [src] => http://userimages02.website.com/userdata/21582754/badge_52780efa210a1cbc468dc627a38c88d8.gif
            [title] => 5
            [id] => 21582754
        )

    [3] => Array
        (
            [src] => http://userimages01.website.com/userdata/21582754/badge_9c56d5678c031b775c6b3b24857d403b.gif
            [title] => 4
            [id] => 21582754
        )

    [4] => Array
        (
            [src] => http://userimages01.website.com/userdata/21582754/badge_da55f03e5ea241054773c2903d7920ed.gif
            [title] => 3
            [id] => 21582754
        )

)

答案 1 :(得分:0)

最简单的方法可能是使用DOMDocument并使用getAttribute这样获取属性。

$html="
<img id='badge-21582754-4' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_b9d6a65d9d9f70575971f26f3b302114.gif' alt='1' title='1' style='width:100px; height:100px; left:0px; top:0px'/>
<img id='badge-21582754-5' class='badgeimg' src='http://userimages03.website.com/userdata/21582754/badge_aa7fed804d36a11c149826e22138e3c5.gif' alt='2' title='2' style='width:100px; height:100px; left:100px; top:0px'/>
<img id='badge-21582754-6' class='badgeimg' src='http://userimages02.website.com/userdata/21582754/badge_52780efa210a1cbc468dc627a38c88d8.gif' alt='5' title='5' style='width:40px; height:100px; left:400px; top:0px'/>
<img id='badge-21582754-7' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_9c56d5678c031b775c6b3b24857d403b.gif' alt='4' title='4' style='width:100px; height:100px; left:300px; top:0px'/>
<img id='badge-21582754-8' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_da55f03e5ea241054773c2903d7920ed.gif' alt='3' title='3' style='width:100px; height:100px; left:200px; top:0px'/>
    ";
    $dom=new DOMDocument;
    $dom->loadHTML( $html );
    $col=$dom->getElementsByTagName('img');
    if( $col->length > 0 ){
        foreach( $col as $img ){

            $pttn='@(badge-(\d{8})-\d)@';
            preg_match($pttn,$img->getAttribute('id'),$matches);

            printf('id: %s title: %s src: %s<br />',$matches[2],$img->getAttribute('title'),$img->getAttribute('src'));
        }
    }

要获得8位数ID,简单regexpreg_match就足够了

答案 2 :(得分:0)

如果HTML格式稳定(即所有属性的顺序与示例中的顺序相同),那么一个好的正则表达就足够了。

$myLIST = "
<img id='badge-21582754-4' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_b9d6a65d9d9f70575971f26f3b302114.gif' alt='1' title='1' style='width:100px; height:100px; left:0px; top:0px'/>
<img id='badge-21582754-5' class='badgeimg' src='http://userimages03.website.com/userdata/21582754/badge_aa7fed804d36a11c149826e22138e3c5.gif' alt='2' title='2' style='width:100px; height:100px; left:100px; top:0px'/>
<img id='badge-21582754-6' class='badgeimg' src='http://userimages02.website.com/userdata/21582754/badge_52780efa210a1cbc468dc627a38c88d8.gif' alt='5' title='5' style='width:40px; height:100px; left:400px; top:0px'/>
<img id='badge-21582754-7' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_9c56d5678c031b775c6b3b24857d403b.gif' alt='4' title='4' style='width:100px; height:100px; left:300px; top:0px'/>
<img id='badge-21582754-8' class='badgeimg' src='http://userimages01.website.com/userdata/21582754/badge_da55f03e5ea241054773c2903d7920ed.gif' alt='3' title='3' style='width:100px; height:100px; left:200px; top:0px'/>
";

preg_match_all("/id='[^']+(\d{8})[^']+'.+src='([^']+)'.+title='([^']+)'/", $myLIST, $matches);

print_r(array_slice($matches, 1));

将输出:

Array
(
    [0] => Array
        (
            [0] => 21582754
            [1] => 21582754
            [2] => 21582754
            [3] => 21582754
            [4] => 21582754
        )

    [1] => Array
        (
            [0] => http://userimages01.website.com/userdata/21582754/badge_b9d6a65d9d9f70575971f26f3b302114.gif
            [1] => http://userimages03.website.com/userdata/21582754/badge_aa7fed804d36a11c149826e22138e3c5.gif
            [2] => http://userimages02.website.com/userdata/21582754/badge_52780efa210a1cbc468dc627a38c88d8.gif
            [3] => http://userimages01.website.com/userdata/21582754/badge_9c56d5678c031b775c6b3b24857d403b.gif
            [4] => http://userimages01.website.com/userdata/21582754/badge_da55f03e5ea241054773c2903d7920ed.gif
        )

    [2] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 5
            [3] => 4
            [4] => 3
        )

)