Jump to content

[SOLVED] Split string


shamuraq

Recommended Posts

Hi there,

 

Anyone knows how i can split say eg, 5432 into 5, 4, 3 ,2? The explode() function still needs a delimiter to split meaning i would loose something. I need to split without losing anything.... Thanx in advance...

Link to comment
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};
}

Link to comment
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
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
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
Share on other sites

Are for arrays you use a different syntax,

 

$x1 = rand(1001,9999);
for($i = 0, $j = count($x1)-1; $i < $j; $i++)
{
    echo $x1[$i];
}

 

Still doesn't work... I'm really am sorry guys... I'm just a bit slow... Pls bear with me...

Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.