Jump to content

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

Edited by erme
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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.