fork(1) download
  1. <?php
  2.  
  3. $html = '
  4. <a href="http://d...content-available-to-author-only...n.com/example1.jpg">
  5. <a href="http://d...content-available-to-author-only...n.com/ストスト.jpg">
  6. <a href="http://d...content-available-to-author-only...n.com/example3.jpg">
  7. ';
  8.  
  9. $images = ma_getImageUrlsFromString($html, $domain);
  10.  
  11. print_r($images);
  12.  
  13.  
  14. function ma_getImageUrlsFromString($html, $domain) {
  15.  
  16. $regex = '#([a-z,:=\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif))#i';
  17.  
  18. preg_match_all($regex, $html, $matches);
  19. $matches = array_unique($matches[0]);
  20. usort($matches, function($a, $b) {
  21. return strlen($b) - strlen($a);
  22. });
  23.  
  24. // convert relative to absolute URLs
  25. $matches_abs_urls = array();
  26. if (!empty($matches) && is_array($matches)) {
  27. foreach ($matches as $match) {
  28. if (0 === strpos($match, '//')) {
  29. // It starts with '//', let's add http:
  30. $matches_abs_urls[] = 'https:'. $match;
  31. } elseif (0 === strpos($match, '/')) {
  32. // It starts with '/', let's add base domain
  33. $matches_abs_urls[] = 'https://' .$domain .''. $match;
  34. } else {
  35. $matches_abs_urls[] = $match;
  36. }
  37. }
  38. }
  39.  
  40. return $matches_abs_urls;
  41. }
  42.  
Success #stdin #stdout #stderr 0.02s 24716KB
stdin
Standard input is empty
stdout
Array
(
    [0] => http://d...content-available-to-author-only...n.com/example1.jpg
    [1] => http://d...content-available-to-author-only...n.com/example3.jpg
    [2] => .jpg
)
stderr
PHP Notice:  Undefined variable: domain in /home/UXgMyQ/prog.php on line 9