473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

foreach and str_replace

So I've been trying to get a bit of code to:
Read all of the files in a dir called thumbs, but not the . and .., use
the filename in a link to get the same filename in an images dir. Now
I'm trying to use a foreach and glob as suggested, and get rid of the
part of the string that has the dir info. SO the below produces all my
thumbs nicely and all are links.

<?php
foreach (glob("thumbs/*.jpg") as $file)
{
str_replace('thumbs/', '', $file);
echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
}
?>

But why doesn't the sring manipulatioon not work? I'm very grateful for
the line with the str_replace. If i remove the braces above i get the
last thumb and a bad link. Replace braces and I get all my thumbs. But
the link still has thumbs/ in it.

hmmm.

thx..ron

Sep 16 '07 #1
3 3427
On Sun, 16 Sep 2007 23:36:49 +0200, Confused but working on it
<Po**********@wherever.comwrote:
So I've been trying to get a bit of code to:
Read all of the files in a dir called thumbs, but not the . and .., use
the filename in a link to get the same filename in an images dir. Now
I'm trying to use a foreach and glob as suggested, and get rid of the
part of the string that has the dir info. SO the below produces all my
thumbs nicely and all are links.

<?php
foreach (glob("thumbs/*.jpg") as $file)
{
str_replace('thumbs/', '', $file);
This does NOT alter $file. $file is not taken as a reference and altered
inside str_replace();

$file = str_replace('thumbs/', '', $file);
echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
}
?>

But why doesn't the sring manipulatioon not work? I'm very grateful for
the line with the str_replace. If i remove the braces above i get the
last thumb and a bad link. Replace braces and I get all my thumbs. But
the link still has thumbs/ in it.

hmmm.

thx..ron
Why do your images live in a thumbs dir and you still want an '/images/'
link?

However, this may help you:

<?php
foreach (glob("thumbs/*.jpg") as $file){
$filename = basename($file);
echo "<a href='images/$filename'><img src='$filename'
class='pad1em'></a>";
}
?>
--
Rik Wasmus
Sep 16 '07 #2
On Sep 16, 5:36 pm, Confused but working on it
<PostInGro...@wherever.comwrote:
So I've been trying to get a bit of code to:
Read all of the files in a dir called thumbs, but not the . and .., use
the filename in a link to get the same filename in an images dir. Now
I'm trying to use a foreach and glob as suggested, and get rid of the
part of the string that has the dir info. SO the below produces all my
thumbs nicely and all are links.

<?php
foreach (glob("thumbs/*.jpg") as $file)
{
str_replace('thumbs/', '', $file);
echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
}
?>

But why doesn't the sring manipulatioon not work? I'm very grateful for
the line with the str_replace. If i remove the braces above i get the
last thumb and a bad link. Replace braces and I get all my thumbs. But
the link still has thumbs/ in it.

hmmm.

thx..ron
You're not doing anything with the return value of str_replace().

I think you want:

$file = str_replace('thumbs/', '', $file);

Sep 16 '07 #3
On 2007-09-16 14:58:48 -0700, ZeldorBlat <ze********@gmail.comsaid:
On Sep 16, 5:36 pm, Confused but working on it
<PostInGro...@wherever.comwrote:
>So I've been trying to get a bit of code to:
Read all of the files in a dir called thumbs, but not the . and .., use
the filename in a link to get the same filename in an images dir. Now
I'm trying to use a foreach and glob as suggested, and get rid of the
part of the string that has the dir info. SO the below produces all my
thumbs nicely and all are links.

<?php
foreach (glob("thumbs/*.jpg") as $file)
{
str_replace('thumbs/', '', $file);
echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
}
?>

But why doesn't the sring manipulatioon not work? I'm very grateful for
the line with the str_replace. If i remove the braces above i get the
last thumb and a bad link. Replace braces and I get all my thumbs. But
the link still has thumbs/ in it.

hmmm.

thx..ron

You're not doing anything with the return value of str_replace().

I think you want:

$file = str_replace('thumbs/', '', $file);
I do dumb things like that. :)

thx..ron

Sep 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jon | last post by:
Why does the following not replace the ":)"? str_replace(":)", "&nbsp;<img src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST); But, the following does replace the ":("? ...
9
by: Hugo Coolens | last post by:
I need to use the str_replace function for a php-script which works together with html/javascript. In the script I have to perform things like: $arabic=str_replace ("'","\'", $row);...
1
by: marcomputers | last post by:
Hi, I'm trying to automatically update an existing rss podcast file using php and the str_replace function. Every time I add a media file, it should read the .rss file into a string, take the...
9
by: Wayne | last post by:
$a = $_POST; # txt_content = This is a<CR><LF>Test $p = str_replace ("%0D%0A", "<br>", $a); That is the above code that I am using, however, it is not picking up the CR/LF from the textarea. I...
4
by: The Numerator | last post by:
I have this page (print.php). In that file, I want to to include a text file from "songs/breathe.txt", and replace all instances of "<b" with "<span" and "</b>" with "</span>". I know how I should...
4
by: d1srupt3r | last post by:
I am developing my form into a captcha secured form and I used header() function if the user didn't put the verification code properly and still when the user go back to the form page the filled he...
5
by: jmark | last post by:
I saw this example in php.net // Outputs: apearpearle pear $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; $output = str_replace($letters, $fruit, $text);...
15
by: fjm | last post by:
Hello everyone and happy Sunday. :) I have a problem that I *think* I may know the solution to but have no idea how to write the code for it. I am working on a templating system wher I have...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.