0

I saw this particular question asked here

My issue is slightly different.

Lets say I have a random integer ( could be any number )

Like so: $rank=123456

Equally it could be $rank=2876545672

What I want to do is split the integer into an array dynamically, and give each value a class.

so it would grab in the example: 123456 the first number and give assign a var like digit-<?=$num['id']

So I could then generate something like:

<span class="digit-1">1</span>
<span class="digit-2">2</span>
<span class="digit-3">3</span>
<span class="digit-4">4</span>
<span class="digit-5">5</span>
<span class="digit-6">6</span>

Is this possible, and if so any ideas how to achieve this ? As tha bove ( spans ) would need to act dynamically so that they are created based on whatever number was generated.

Driving me nuts trying to figure it out.

2

3 Answers 3

1

Try this out:

<?php

$rank = 123456;
$div = str_split($rank);

foreach ($div as $key) {
    echo '<span class="digit-', $key, '">', $key, '</span>';
} 

?>

I used commas instead of dots as the code loads faster.

Sign up to request clarification or add additional context in comments.

2 Comments

OK, in that case don't forget the check :D
Here it is working live ( thought I would share so you could see your code in action ) 422.com.au/about/growth
1
<?php

    $rank = 123456;
    $numbers = str_split($rank."");

    foreach($numbers as $n) {
        echo '<span class="digit-'.$n.'">'.$n.'</span>'."\n";
    }

?>

Comments

0

not sure I understand you correctly but:

$test = 12345;
$test = (string) $test;
for($i = 0; $i < strlen($test); $i++){
    print("<span class=\"digit-{$test[$i]}\">{$test[$i]}</span>");
}

1 Comment

In this case is faster and better use foreach instead of for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.