Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts

    Text-numbers to images

    Hello.

    I have a script that counts how many users that exist in the system. Instead of printing it as text, I'd like to show them as images. I have the numbers 0 to 9 in .png files.

    What would be the easiest way to split up "132" and make three different <img> tags for each individual number?

    Code:
    <img src="1.png" /><img src="3.png" /><img src="2.png" />
    Thanks
    Last edited by [vengeance]; 11-06-2010 at 12:06 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    You mean, if you got 132 in a variable, how to break it up?

    Convert it to a string and treat it like an array:

    PHP Code:
    $count 132;
    $count = (string) 132;

    foreach (
    $count as $number) {
    echo 
    "<img src=\"{$number}.png\">";


  • #3
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Keleth View Post
    You mean, if you got 132 in a variable, how to break it up?

    Convert it to a string and treat it like an array:

    PHP Code:
    $count 132;
    $count = (string) 132;

    foreach (
    $count as $number) {
    echo 
    "<img src=\"{$number}.png\">";

    Do you mind explaining deeper what you're doing there?

    Also, it gives me the following:
    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\project\index.php on line 86

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Here, try this:
    PHP Code:
    $numbersArray str_split((string) $count);
    foreach(
    $numbersArray as $num)
    {
       echo 
    '<img src="' $num '.png" />'

    It's splitting the count into an array of the individual numbers, then looping through them and printing out an image tag for each one.


    Could also do it this way:
    PHP Code:
    $count 132;
    $count = (string) $count;
    $countLen strlen($count);
    for(
    $i 0$i $countLen$i++)
    {
       echo 
    '<img src="' $count[$i] . '.png" />'

    Last edited by Inigoesdr; 11-06-2010 at 12:00 AM.

  • Users who have thanked Inigoesdr for this post:

    [vengeance] (11-06-2010)

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    I fail at testing! I did a small quick check and didn't bother to actually test the code I gave you.

    Quick fix, though more complicated!

    PHP Code:
    $count 132;
    preg_match_all('/\d/'$count$matches); 
    $matches[0] will contain your results.

    Before what I was doing was typecasting $count (your number) as a string. If you echo $stringName[3] (randomly location in a string), it'll echo out the character in that position. I thought, hey, we can do that with foreach! But foreach doesn't like strings, so we go to instead, for!

    PHP Code:
    $count 132;
    $count = (string) $count;

    for (
    $position 0$position strlen($count); $position++) {
    echo 
    "<img src=\"{$count[$position]}.png\">";

    I do like regex better through

    Edit: Also, Inigoesdr is smarter then me

  • Users who have thanked Keleth for this post:

    [vengeance] (11-06-2010)

  • #6
    Regular Coder
    Join Date
    Aug 2009
    Posts
    131
    Thanks
    28
    Thanked 7 Times in 7 Posts
    Thanks both of you. Was exactly what I was looking for.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •