简体   繁体   中英

PHP get all links and images with preg_match_all

from news website we have many tag like with :

<a class="picLink" target="_blank" href="/EN/news/397423/MY-TEST-NEWS">
   <img class="fr" width="115" style="margin:6px 0px 0px 8px;width: 115px;" src="http://sample.com/files/EN/news/369326_276.jpg" alt="MY TEST NEWS !">
</a>

i want to get href and image src and alt attribute values from that.

my simple code do not correct:

<meta charset='UTF-8' />
<?php
error_reporting(1);
$handle='http://sample.com';
$handle = file_get_contents($handle);
preg_match_all('/<a[^>]+class="titr1y"[^>] href="(.*?)"*><img class="[^>]" width="[^>]" style="[^>]" src="(.*?)" alt="(.*?)"*><\/a>/si', $handle, $matching_data);

print_r($matching_data);
?>

please help me to correct that.

you can get it with this

$re = '/(alt|href|src)=("[^"]*")/'; 
$str = '<a class="picLink" target="_blank" href="/EN/news/397423/MY-TEST-NEWS">\n   <img class="fr" width="115" style="margin:6px 0px 0px 8px;width: 115px;" src="http://sample.com/files/EN/news/369326_276.jpg" alt="MY TEST NEWS !">\n</a>\n<a class="picLink" target="_blank" href="/EN/news/397423/MY-TEST-NEWS">\n   <img class="fr" width="115" style="margin:6px 0px 0px 8px;width: 115px;" src="http://sample.com/files/EN/news/369326_276.jpg" alt="MY TEST NEWS !">\n</a>'; 

preg_match_all($re, $str, $matches);
print_r($matches);

output

(
    [0] => Array
        (
            [0] => href="/EN/news/397423/MY-TEST-NEWS"
            [1] => src="http://sample.com/files/EN/news/369326_276.jpg"
            [2] => alt="MY TEST NEWS !"
            [3] => href="/EN/news/397423/MY-TEST-NEWS"
            [4] => src="http://sample.com/files/EN/news/369326_276.jpg"
            [5] => alt="MY TEST NEWS !"
        )

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

    [2] => Array
        (
            [0] => "/EN/news/397423/MY-TEST-NEWS"
            [1] => "http://sample.com/files/EN/news/369326_276.jpg"
            [2] => "MY TEST NEWS !"
            [3] => "/EN/news/397423/MY-TEST-NEWS"
            [4] => "http://sample.com/files/EN/news/369326_276.jpg"
            [5] => "MY TEST NEWS !"
        )

)

in array[2] you'll get the desired values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM