Jump to content

[SOLVED] Split string


shamuraq

Recommended Posts

You can access each individual character within a string using this

 

$string = '12345';

echo $string{3}; // get the 4th character

echo $string{1}; // get the 2nd character

echo $string{4}; // get the 5th character

 

Alternatively you can use a loop:

 

for($i = 0, $j = strlen($string); $i < $j; $i++)
{
    echo $string{$i};
}

Link to comment
https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834255
Share on other sites

You can access each individual character within a string using this

 

$string = '12345';

echo $string{3}; // get the 4th character

echo $string{1}; // get the 2nd character

echo $string{4}; // get the 5th character

 

Alternatively you can use a loop:

 

for($i = 0, $j = strlen($string); $i < $j; $i++)
{
    echo $string{$i};
}

 

so does that mean i can split them and store them as individual integer. Eg, 54321 into $a, $b,$c, $d, $e? Where $a = 5; $b = 4 etc...

Link to comment
https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834260
Share on other sites

Yes that is achievable, however this method will only work with strings. Why are you wanting to store each digit within a separate variable?

 

Some math equation randomiser i need for a 3rd grader class... you know where they can learn about numbers with the position of ones, tens, hundreds and thousands. The class is a charitable cause for orphans and low income families. Since its a continuous effort i just need a script so that i can just print it on the run... Thanx again m8...

Link to comment
https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834282
Share on other sites

btw wildteen88,

i tried your:

for($i = 0, $j = strlen($string); $i < $j; $i++)

{

    echo $string{$i};

}

it doesn't work. just blank screen

 

my code goes :

    <?

$x1 = rand(1001,9999);

for($i = 0, $j = strlen($x1); $i < $j; $i++)

{

    echo $x1{$i};

}

?>

Anything wrong?

Link to comment
https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834287
Share on other sites

wow, you make that look hard... lol

<?php
$x1 = (string) rand(1001, 9999);//lowest number, highest number
$digits = str_split($x1);//spilits it into digits
echo "The number $x1<br><br>";

$places = array("Ones", "Tens", "Hundreds", "Thousands", "Ten thousands", "Hundred thousands");
foreach($digits as $place=>$digit){
echo $places[$place]." is ".$digit."<br>";
}
?>

Have fun with that one, let me know if you want it to work differently. The educational system needs more interaction with computers.

Link to comment
https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834332
Share on other sites

Sorry I mis read your code earlier. I thought you was using range at the time.

 

rand() returns a number between 1001 and 9999. To break the number into ones, tends, hundreds, thousands I came up with this:

<?php

$x1 = (string) rand(1000, 9999);

$fields = array('ones', 'tens', 'hundreds', 'thousands');

$digits = str_split($x1);

echo '<p>The number: '. number_format($x1, ',') .'</p>';

?>
<table border="1" cellspacing="1" cellpadding="10">
  <tr>
     <?php foreach($fields as $field)
           {
               echo '<th>'.ucwords($field).'</th>';
           }
     ?>
  </tr>
  <tr>
     <?php
           foreach(array_reverse($digits) as $num)
           {
               echo '<td>'.$num.'</td>';
           }
     ?>
  </tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834341
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.