Jump to content

[SOLVED] Tweak code into an array...


jsschmitt

Recommended Posts

Ok... I have been playing with some code I found that pulls the thumbnail from Google images, but I want it to pull more then one image.

 

I know I will need an array, I just can't figure out where to place it.

 

Or maybe a loop?

 

Any help?

 

<?php

function google_image ($q)
{
  $q_filter = "*.*";
  $q = str_replace ( " " , "+", $q );
  $get_data = file_get_contents ("http://images.google.com/images?q=".$q . "+" . $q_filter . "&imgsz=small|medium|large|xlarge&hl=en&sa=G&safe=off&gbv=1");
  $exp1 = explode ( "http://tbn", $get_data );
  $exp2 = explode ( "width", $exp1[1] );
  return "http://tbn" . $exp2[0];
}

$image = google_image ("criteria");
echo "<img src='$image'>";

?>

Link to comment
https://forums.phpfreaks.com/topic/164681-solved-tweak-code-into-an-array/
Share on other sites

This might work:

 

<?php

function google_image ($q)
{
  $q_filter = "*.*";
  $q = str_replace ( " " , "+", $q );
  $get_data = file_get_contents ("http://images.google.com/images?q=".$q . "+" . $q_filter . "&imgsz=small|medium|large|xlarge&hl=en&sa=G&safe=off&gbv=1");
  $exp1 = explode ( "http://tbn", $get_data );
  
  $images = array();
  
  foreach ($exp1 as $exp)
  {
$exp2 = explode ( "width", $exp );
$images[] = "http://tbn" . $exp2[0];
  }
  
  return $images;
}

$images = google_image ("criteria");
foreach ($images as $image)
{
echo "<img src='$image'>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.