Selectiong characters from an integer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Selectiong characters from an integer

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

$numbers = "129019289739749357";

echo substr($numbers, 0, 4);
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post 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]
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

<?php
$str = '123';
echo $str[2];  //3
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_split() is php 5 only, without using the compatibility layer from pear.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post 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!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Of course you can't duk.. there's no character at position 3 in the resulting string. Why should there be?
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've read your post. It doesn't answer the question.

Your questions were answered though.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

ok if yes i didn't understand... its not very important anyway
:D

but i put $some[2] ok its wrong but if we put $some[1] will not exist to...
Post Reply