fork download
  1. <?php
  2.  
  3. $string = '
  4. http://i...content-available-to-author-only...a.gov/image/image_launch_a5.jpg
  5. http://p...content-available-to-author-only...r.fr/programmation/images/mozodojo-original-image.jpg
  6. http://i...content-available-to-author-only...a.gov/image/image_launch_a5.jpg
  7.  
  8. Alot of text
  9.  
  10. http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif
  11.  
  12. more text';
  13.  
  14. $urls = array();
  15. $lines = explode(PHP_EOL,$string);
  16. foreach ($lines as $line){
  17. $line = trim($line);
  18.  
  19. // ignore empty lines
  20. if (strlen($line) === 0) continue;
  21.  
  22. $pUrl = parse_url($line);
  23.  
  24. // non-valid URLs don't count
  25. if ($pUrl === false) break;
  26.  
  27. // also skip URLs that aren't images
  28. if (stripos($pUrl['path'],'.jpg') !== (strlen($pUrl['path']) - 4)) break;
  29.  
  30. // anything left is a valid URL and an image
  31. // also, because a non-url fails and we skip empty lines, the first line
  32. // that isn't an image will break the loop, thus stopping the capture
  33. $urls[] = $line;
  34. }
  35. var_dump($urls);
  36.  
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
array(3) {
  [0]=>
  string(52) "http://i...content-available-to-author-only...a.gov/image/image_launch_a5.jpg"
  [1]=>
  string(78) "http://p...content-available-to-author-only...r.fr/programmation/images/mozodojo-original-image.jpg"
  [2]=>
  string(52) "http://i...content-available-to-author-only...a.gov/image/image_launch_a5.jpg"
}