Forum Moderators: coopster
Best Regards
Mahoney
[edited by: coopster at 7:39 pm (utc) on Feb. 9, 2005]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]
$url = 'http://www.myserver.com/images/[003-008].jpg';
list ($url2, $ending) = split ('[', $url);
This now gives you two variables as such:
url2 = "http://www.myserver.com/images/"
ending = "003-008].jpg"
Note: if the script fails (I haven't checked it) try escaping the character you are splitting the string by, using a slash before it. Eg:
list ($url2, $ending) = split ('[b]\[/b][', $url);
Now to split that ending down further.
list ($no1, $almost) = split ('-', $ending);
Now we have this:
no1 = "003"
almost = "008].jpg"
Probably best to chop the end off, but you could use split again like this:
list ($no2) = split (']', $almost);
Now we get this:
no2 = "008"
Is this helpful?
list ($domain, $ending) = split ('\[',$url);
list ($start_no, $almost) = split ('-',$ending);
list ($end_no, $filetype) = split ('\]', $almost);
$count = $start_no;
while ($count <= $end_no) {
echo "<img src='".$domain,$count,$filetype."'><br><br><br>";
$count = $count + 1; }
Now it works fine but when I am adding a series of images with leading zeros like so:
[myserver.com...]
it takes away the leading zeros after the first loop so it only displays:
[myserver.com...]
[myserver.com...]
[myserver.com...]
How can I keep the leading zeros there so i can add the url [myserver.com...]
and it would display all the urls correctly. e.g.
[myserver.com...]
[myserver.com...]
[myserver.com...]
etc etc
[myserver.com...]
[myserver.com...]
etc etc
[myserver.com...]
[myserver.com...]
Best regards
Mahoney
echo "<img src='$src'><br><br><br>";
If you want to echo directly, use printf() [php.net] instead.
You *may* start to run into problems if you get more than 999 images though. Luckily, printf() does not truncate so
printf('%03d', 1032034);
outputs: 1032034 rather than 103.
This works well but
when i input:
[myserver.com...] it makes
[myserver.com...]
[myserver.com...]
[myserver.com...]
etc etc
[myserver.com...]
[myserver.com...]
how can i get the script to detect how many leading zeros the first number has and makes sure it prints out the correct solution. e.g
[myserver.com...] becomes
[myserver.com...]
[myserver.com...]
[myserver.com...]
.....
[myserver.com...]
[myserver.com...]
Regards
Mahoney
while ($count <= $end_no) {
echo "<img src='".$domain,$count,$filetype."'><br><br><br>";
$count = $count + 1; }Now it works fine but when I am adding a series of images with leading zeros... it takes away the leading zeros after the first loop.
The first $count is a string, say "001". But then you're adding to it so I guess it acts like a number afterwards, eg "2" rather than "002". If the range of numbers never misses a step then you could simply use a loop from the first number to the last one, and generate the string manually. Eg:
start no. = 001
end no. = 012
loop goes from 1 to 12
output loop value adding zeros depending on length
So if the loop was at 9, you'd add 2 zeros to get "009".
I'm not sure if this would work with all the numbers you might have, but it might be one way to do this.
For example, with
ht*p://www.example.com/images/[01-13].jpg
$start_no = 01;
That will have a string length of 2, so our pattern for printf should be '%02d';
$string = '07';
$pattern = '%0' . strlen($string) . 'd';
printf($pattern, 1);
outputs: 01
$string = '00007';
$pattern = '%0' . strlen($string) . 'd';
printf($pattern, 1);
outputs: 00001