-1

My code bellow grabs some content. in this content there are some photos. How can i loop through this content find all images and return their src?

my code so far:

$items = $html->find('div.post-single-content',0)->children(1)->outertext;
foreach($items $node) { 
$node->find('img');
}
print_r ($node);
jfriend00
  • 683,504
  • 96
  • 985
  • 979
Irene T.
  • 1,393
  • 2
  • 20
  • 40

1 Answers1

4

Don't use regex, use a parser. Example:

$string = '<img src="You want this" style="width:200px;" />';
$doc = new DOMDocument();
$doc->loadHTML($string);
$images = $doc->getElementsByTagName('img');
foreach ($images as $image) {
     echo $image->getAttribute('src') . "\n";
}

Output:

You want this

chris85
  • 23,846
  • 7
  • 34
  • 51
  • EXCELLENT @chris85 !!!!!! I have one question... how easy is now to upload the photos to folder "images" and replace each of them with new uploaded url? – Irene T. Oct 03 '15 at 22:44
  • I'm not sure exactly what you mean. This is only pulling the `src`s from an `HTML` page... maybe the `rename` function is what you are looking for, http://php.net/manual/en/function.rename.php? – chris85 Oct 03 '15 at 22:49
  • Lets say that we have the string $string = 'kitty and dog '; How is it possible to upload this 2 photos to another directory and replace each src with the new (new upload directory). Images are from another website and i want to upload it to my server – Irene T. Oct 03 '15 at 22:52
  • 1
    Are you trying to copy images from a server that isn't yours to your server, or are the images already on your server and you want to move them? If the former,http://stackoverflow.com/questions/724391/saving-image-from-php-url. If the latter take off the text preceding the TLD and replace it with the server path to the web dir. Then use the rename function. – chris85 Oct 03 '15 at 22:57
  • check this out http://stackoverflow.com/questions/32928588/find-all-images-upload-them-and-replace-with-new – Irene T. Oct 03 '15 at 23:48