erme Posted February 13, 2013 Share Posted February 13, 2013 (edited) 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 February 13, 2013 by erme Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/ Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 Probably. Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412263 Share on other sites More sharing options...
erme Posted February 13, 2013 Author Share Posted February 13, 2013 If i were to explode it, i would need to say ignore everything before /img Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412268 Share on other sites More sharing options...
shlumph Posted February 13, 2013 Share Posted February 13, 2013 Or replace the first part with an empty string: http://www.php.net/str_replace Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412270 Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 If you were to post your code we could help you. Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412272 Share on other sites More sharing options...
erme Posted February 13, 2013 Author Share Posted February 13, 2013 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412274 Share on other sites More sharing options...
shlumph Posted February 13, 2013 Share Posted February 13, 2013 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="">'; } Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412276 Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412277 Share on other sites More sharing options...
erme Posted February 13, 2013 Author Share Posted February 13, 2013 Thanks I'll give these a go Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412291 Share on other sites More sharing options...
erme Posted February 14, 2013 Author Share Posted February 14, 2013 Works perfectly. Thanks very much! Quote Link to comment https://forums.phpfreaks.com/topic/274449-remove-part-of-a-foreach/#findComment-1412403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.