PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > foreach and str_replace
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
foreach and str_replace

Réponse
 
LinkBack Outils de la discussion
Vieux 16/09/2007, 22h36   #1
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 16/09/2007, 22h58   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: foreach and str_replace

On Sun, 16 Sep 2007 23:36:49 +0200, Confused but working on it
<PostInGroups@wherever.com> wrote:

> 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 you:

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


--
Rik Wasmus
  Réponse avec citation
Vieux 16/09/2007, 22h58   #3
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: foreach and str_replace

On Sep 16, 5:36 pm, Confused but working on it
<PostInGro...@wherever.com> wrote:
> 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);

  Réponse avec citation
Vieux 16/09/2007, 23h10   #4
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: foreach and str_replace

On 2007-09-16 14:58:48 -0700, ZeldorBlat <zeldorblat@gmail.com> said:

> On Sep 16, 5:36 pm, Confused but working on it
> <PostInGro...@wherever.com> wrote:
>> 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

  Réponse avec citation
Vieux 16/09/2007, 23h17   #5
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: foreach and str_replace

On 2007-09-16 14:58:31 -0700, "Rik Wasmus" <luiheidsgoeroe@hotmail.com> said:
>
> This does NOT alter $file. $file is not taken as a reference and altered
> inside str_replace();
>
> $file = str_replace('thumbs/', '', $file);


Yeah, that was dumb.

>> thx..ron

>
> Why do your images live in a thumbs dir and you still want an '/images/'
> link?
>
> However, this may you:
>
> <?php
> foreach (glob("thumbs/*.jpg") as $file){
> $filename = basename($file);
> echo "<a href='images/$filename'><img src='$filename'
> class='pad1em'></a>";
> }
> ?>
>
>
> --
> Rik Wasmus


My thumbs live in thumbs and the full size in images. Made sense to me.
I export from iPhoto a set of thumbs and then the larger pics.

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

Works great. I got those file markers and realized you took out the
"thumbs/" in the echo statement.

Uploaded and the files are in order of filename.

Next week maybe I'll learn how to take a directory named images and
create thumbs on the fly. Might hve been easier.

Thanks for all the .

Ron

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13206 seconds with 13 queries