Reputation: 11
The variable $myLIST gives the following output.
<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'/>
How can I extract the SRC links, the TITLES and the 8 digit number inside the IMG ID for each output? Thanks!
LE. Thanks everyone, problem solved!
Upvotes: 0
Views: 56
Reputation: 33813
The easiest perhaps would be to use DOMDocument
and get the attributes using getAttribute
like this.
$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'));
}
}
To get the 8 digit id a simple regex
and preg_match
should suffice
Upvotes: 0
Reputation: 3646
One good Regular Expression is enough if the HTML has stable format (i.e. all properties always go in the same order as in your example).
$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));
Will output:
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
)
)
Upvotes: 0
Reputation: 54
You can use 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);
Output :-
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
)
)
Upvotes: 2