Page 1 of 1
Selectiong characters from an integer
Posted: Tue Feb 21, 2006 4:49 am
by AshrakTheWhite
Hello
Heres the problem i have, id like to take a certain ammount of characters from a string and print em out seperately.
for instance if i have a number like 129019289739749357 then if i wanted it to select the 4 first numbers out of that entire thing it would print out 1290
is there any way of doing that?
Posted: Tue Feb 21, 2006 4:58 am
by JayBird
Code: Select all
$numbers = "129019289739749357";
echo substr($numbers, 0, 4);
Posted: Tue Feb 21, 2006 8:02 am
by duk
i was thinking where goes the other numbers ???
because i use this function sometimes, i saw this post and ask myself and if i want to use the rest of the numbers...
i do some testes and substr() doesn't put the numbers in arrays..
Code: Select all
$numbers = 123456789;
$get_number = substr($numbers, -1, 1 );
var_dump($get_number);
you get:
string(1) "9"
the only way to use the other numbers is to do again a calculation with the substr($numbers, 0, 8 ); ??? or there is ther function that divides and put in arrays ???
and use like $get_number[0] and then you use for the other numbers $get_number[1]
Posted: Tue Feb 21, 2006 9:38 am
by josh
Code: Select all
<?php
$str = '123';
echo $str[2]; //3
?>
Posted: Tue Feb 21, 2006 9:58 am
by s.dot
hmm
Code
Code: Select all
$str = 12345678910;
$str_array = str_split($str,4);
print_r($str_array);
Output:
Code: Select all
Array (
[0] => 1234
[1] => 5678
[2] => 910
)
Posted: Tue Feb 21, 2006 10:02 am
by feyd
str_split() is php 5 only, without using the
compatibility layer from pear.
Posted: Wed Feb 22, 2006 2:08 am
by duk
jshpro2 wrote:Code: Select all
<?php
$str = '123';
echo $str[2]; //3
?>
nahh what im saying is after you do
Code: Select all
$some = substr($str, 0, 1); // for example
then you can't do $some[2] thats my point!!!
Posted: Wed Feb 22, 2006 2:20 am
by feyd
Of course you can't duk.. there's no character at position 3 in the resulting string. Why should there be?
Posted: Wed Feb 22, 2006 2:30 am
by duk
duk wrote:i was thinking where goes the other numbers ???
because i use this function sometimes, i saw this post and ask myself and if i want to use the rest of the numbers...
i do some testes and substr() doesn't put the numbers in arrays..
Code: Select all
$numbers = 123456789;
$get_number = substr($numbers, -1, 1 );
var_dump($get_number);
you get:
string(1) "9"
the only way to use the other numbers is to do again a calculation with the substr($numbers, 0, 8 ); ??? or there is ther function that divides and put in arrays ???
and use like $get_number[0] and then you use for the other numbers $get_number[1]
please see this post...
Posted: Wed Feb 22, 2006 2:39 am
by feyd
I've read your post. It doesn't answer the question.
Your questions were answered though.
Posted: Wed Feb 22, 2006 3:01 am
by duk
ok if yes i didn't understand... its not very important anyway
but i put $some[2] ok its wrong but if we put $some[1] will not exist to...