Web Hosting Talk







View Full Version : PHP some code help needed please


UH-Matt
01-24-2006, 06:14 PM
Im not very good with PHP but I want to try and do this.

Lets assume I have a url to a text file containing a number,

www.somedomain.com/somefile.txt

This file may contain a number 9234 or 10394 or anything else.

I want some php code that will read that text file number, and then create a graphic of that number using individual number files.

So I may have:

1.gif , 2.gif , 3.gif , 4.gif , 5.gif , 6.gif , 7.gif , 8.gif , 9.gif , 0.gif , comma.gif

--

So the code will read that text file and see the number is 10123 and it will build the following code:

<img src="images/1.gif"><img src="images/0.gif"><img src="images/comma.gif"><img src="images/1.gif"><img src="images/2.gif"><img src="images/3.gif">

This would in turn build an image of 10,123 out of the images i have created....

Im sure you understand what i need ;) any code appreciated.

Dan L
01-24-2006, 06:25 PM
<?php
$file = file_get_contents('somefile.txt');
$file = explode("\\",$file);
foreach($file as $key => $value) {
echo "<img src=\"$value.gif\" />";
}
?>

There are better ways of doing what you described, but the above should work.

UH-Matt
01-24-2006, 06:26 PM
What about adding the comma in the right place to seperate thousands? :)

Dan L
01-24-2006, 06:29 PM
<?php
$file = file_get_contents('somefile.txt');
$file = number_format($file);
$file = explode("\\",$file);
foreach($file as $key => $value) {
if($value == ',') {
$value = 'comma';
} elseif($value == '.') {
$value = 'dot';
}
echo "<img src=\"$value.gif\" />";
}
?>

UH-Matt
01-25-2006, 12:04 AM
Ok

The text file im reading doesnt have a comma, but I want the final image to display the comma in the right place.

So now the code needs to take the number and work out how many digits are in it, and work out if/where a comma image should be displayed...

:)

Burhan
01-25-2006, 02:35 AM
$number = number_format(trim(file_get_contents("number.txt")));
$size = strlen($number);
for($x = 0; $x < $size; $x++)
{
if ($number{$x} === ',')
{
echo '<img src="comma.gif">';
} else {
echo '<img src="'.$number{$x}.'.gif">';
}
}

Dan L
01-25-2006, 03:28 PM
fyrestrtr's way is better, the trim() is important.

FYI, number_format automatically adds the commas into the number.