0

$value is a string of URLs. Each URL hotlinks to a JPG file and this code displays those on a page. Each URL is separated by a comma.

If I only want to display the first 1 or 2 images, what do I do? I tried to add a limiter to the explode function but I think I'm missing something because it doesn't quite work

$images = explode(',', $value);

$value = '';
foreach ($images as $im){
    $value .= '<img src="'. $im . '" />';
}
1
  • var_dump($value) ? Commented Sep 14, 2015 at 16:19

1 Answer 1

5

array_slice is your friend!

$images = array_slice(explode(',', $value), 0, 2);
//extracts 2 elements, starting with index 0
$value = '';
foreach ($images as $im){
    $value .= '<img src="'. $im . '" />';
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm happy that I could help you! Please consider accepting my answer if it solves your question ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.