Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Splitting a string in PHP

  • 15-11-2007 5:57pm
    #1
    Registered Users, Registered Users 2 Posts: 35,524 ✭✭✭✭


    I am slowly (very slowly..) learning PHP and there is something I'd like to be able to do which involves splitting a string of numbers.

    I have an outputted number eg: 146
    I would like to turn this into: <img src="1.jpg" /><img src="4.jpg" /><img src="6.jpg" />

    I've looked into explode and split functions but all of them seem to need a delimiter in the string.

    Anybody any ideas as to what php function I need to Google?


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    split() should still work without a delimter. OK, well not without one, but with an empty string, i.e.

    split("", myString);

    You should also be able to iterate through the string and get each character in turn;
    for($i = 0; $i < strlen(myString); $i++) {
    $char = substr(myString, $i, 1);
    echo '<img src="$char.jpg" />';
    }
    


  • Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭egan007


    <?php


    $string = "12345";

    $chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);

    foreach($chars as $image){
    $list .= "<img src=\"./$image.jpg\">";
    }
    echo $list;

    ?>


  • Registered Users, Registered Users 2 Posts: 35,524 ✭✭✭✭Gordon


    You guys rock, thank you so much :)

    Now I'm going to have to dissect both of your codes and try to understand why they both work and how they both work!

    Cheers.


Advertisement