Help with a smart function.

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
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Help with a smart function.

Post by lovelf »

Hello, could I assign a value for each character in a string?

For example: "url.php?var=19200"

1, 9, 2, 0, 0

$char1 = 1, $char2 = 9, $char3 = 2, $char4 = 0, $char5 = 0

Same for any value.

I have no clue how to do this.
Last edited by lovelf on Wed Jan 14, 2009 5:24 am, edited 2 times in total.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Help with a smart function.

Post by jaoudestudios »

strlen will give you how long the string is.

To split it up, you could use a for loop with this number and go through the original string character at a time and save them in variables you want.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Help with a smart function.

Post by Mark Baker »

Code: Select all

 
function makeKey(&$item,$key,$prefix) {
   $item = $prefix.$key;
}
 
$string = '19200';
$array_keys = range(1,strlen($string));
array_walk($array_keys,'makeKey','Char');
$varArray = array_combine($array_keys,str_split($string));
 
extract($varArray);
 
Post Reply