Jump to content

Remove part of a foreach


erme

Recommended Posts

I have a result of the following as part of a foreach statement:

 

C:/xampp/htdocs/Site/Folder/img/banner/moo/activities/1.jpg

 

but obviously I can link this image. Is there an easy way to get rid of the first bit

 

C:/xampp/htdocs/Site/Folder

 

so it will display

 

/img/banner/moo/activities/1.jpg

Link to comment
https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/
Share on other sites

<?php
   $uri = $_SERVER['REQUEST_URI'];

   $directory = $_SERVER['DOCUMENT_ROOT'].'/img/banner' . $uri . '/';

   $images = glob($directory . "*.jpg");


   foreach($images as $image)
   {
       $test = explode("/" , $image);

       echo "<pre>".print_r($test)."</pre>";

       echo '<img class="slide" src="' . $image . '" alt="">';
   }
?>

 

Basically it gets all .jpg files from a folder as per the URI. I need document root to call the folder, but don't want it to render the image in HTML

There's more than one way to skin a cat, but this is what I would do:

 


$uri = $_SERVER['REQUEST_URI'];

$directory = $_SERVER['DOCUMENT_ROOT'].'/img/banner' . $uri . '/';

$images = glob($directory . "*.jpg");

foreach($images as $image)
{
   $image = str_replace($_SERVER['DOCUMENT_ROOT'], '', $image);
   echo '<img class="slide" src="' . $image . '" alt="">';
}

<?php
   $uri = $_SERVER['REQUEST_URI'];
$img_folder = '/img/banner';
$directory = $_SERVER['DOCUMENT_ROOT'].$img_folder. $uri . '/';

   $images = glob($directory . "*.jpg");
foreach($images as $image)
   {
$img_path = str_replace($img_folder, '', $image);
echo '<img class="slide" src="' . $img_path. '" alt="">';
   }
?>

 

Try that?

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.