Forum Moderators: coopster

Message Too Old, No Replies

String Handling!

Help me with this script please!

         

Mahoney

2:57 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



I have a very annoying problem, I am trying to cut up an url into pieces for further processing but dont know how.
e.g.
[example.com...]
I want the "http://www.example.com/images/",".jpg","003" and "008" stored in a variable then a count from start(003) to end(008) but also the leading zeros have to stay in place.

Best Regards
Mahoney

[edited by: coopster at 7:39 pm (utc) on Feb. 9, 2005]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

Hester

4:23 pm on Feb 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are several ways to do it. If the variables are always the same length, you can cut the string down to get the values you require. See the String section of the online PHP Manual for the commands needed. I like to use explode() or split though. Split scans a string for a certain character, and gives you everything around it. Eg:


$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?

Mahoney

4:59 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



Thanks alot for the reply, I think this is going to work as I was playing with thesplit function last night but had no luck. Well I'll try this out. :)

Mahoney

9:26 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Thanks for the reply yesterday Hester. Now I have my script working but im missing one more thing.

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

ergophobe

9:56 pm on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$src = $domain . sprintf [php.net]('%03d', $count) . $filetype;

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.

Mahoney

10:06 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Thanks for the reply ergophobe :)

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

Hester

9:58 am on Feb 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

ergophobe

5:35 pm on Feb 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use strlen() [php.net] to get the length and then go from there.

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

Mahoney

2:03 pm on Feb 12, 2005 (gmt 0)

10+ Year Member



Thanks Hester and Ergophobe!

It resulted in this:


while ($count <= $end_no) {
echo "<img src='".$domain . sprintf('%0'.strlen($start_no).'d', $count) . $filetype."'><br><br><br>";
$count += 1; }

Thanks for the help! :)