PHP link

Hi just wondering what i can do to change the output of the path for example…
[php]<?php
$files = glob(“images/.”); for ($i=1; $i<count($files); $i++) {
$num = $files[$i]; print $num."
"; echo ‘random image’."

";
}
?>[/php]

What it does is outputs every image which is in a folder, it also outputs the file name of each image. but … the image file names always say like /images/image001.jpg although i just want it to say… image001.jpg.

Hi.

Though there are easier methods of accomplishing your task, to do it with the same code, you need only start the count at the 9-th character rather than the first character. Example

Rather than … ($i=1; $i<count($files);

do …($i=9; $i<count($files);

This would start the draw from the 9-th character of $files rather than including the first seven characters which are “\images”

Hope this helps.

Well…
[php]$files = glob(“images/.”); for ($i=1; $i<count($files); $i++)
{ $num = $files[$i];
print $num."
";
echo ‘random image’."

";
}

[/php]

When i change the [php]r ($i=1; $i<count($files); $i++) [/php]
It changes which files to display, it only displays the files from the 9th on…

I think i need to edit something in
[php]$files = glob(“images/.”); [/php]
but what can i put in there?

actually it will be in this part as this is displaying the variable.

[php]echo 'random image[/php]

My bad. I did not fully review your purpose for the loop.

Ok. Here is your proper solution:

First get the length of your string…

         $s_len=strlen($nums);

Next subtract 8 (the number of characters in “/images/” from the length of your $numb …
$s_len=$s_len-8

Now Collect the remaining string by picking up the characters from a reverse stand point …

         $newnumb=substr($numb,-$_len);

Here is an example of how this function.

substr("\images\myfilename", -10) returns “myfilename” … ten characters counting from the end of the string.

Going from the other direction, you could also use…
$newstring=substr($numb,10,10);

This would begin at position 10 in $numb and collect 10 characters.

The actual syn is like this:

string substr ( string $string , int $start [, int $length ] )

Sponsor our Newsletter | Privacy Policy | Terms of Service