Page 1 of 1

Help with a smart function.

Posted: Wed Jan 14, 2009 5:07 am
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.

Re: Help with a smart function.

Posted: Wed Jan 14, 2009 5:13 am
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.

Re: Help with a smart function.

Posted: Wed Jan 14, 2009 9:25 am
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);