Page 1 of 1
explode
Posted: Sun Nov 28, 2004 2:39 am
by bobbyaa
I am looking to take a 17 character string and break it down to a 17 element array. There are no specific characters to deliniate when I want a new element to start. Here's an example of what I want to happen.
$VIN=2GH445GH52W334556
I want that to be broke into an array with the following elements :
Code: Select all
<?php
$vin_array[0]="2";
$vin_array[1]="G"; etc......
?>
Any ideas?
Posted: Sun Nov 28, 2004 3:28 am
by jl
Google for 'php string array convert'
returned a reference to this page in the PHP man:
http://uk2.php.net/manual/en/function.str-split.php
Posted: Sun Nov 28, 2004 3:30 am
by rehfeld
php5 has a function for this, but heres a quick n dirty php4 version
Code: Select all
<?php
function str_to_array($string) {
$len = strlen($string);
$str_array = array();
for ($i=0; $i<$len; $i++) {
$str_array[] = $string{$i};
}
return $str_array;
}
?>
Posted: Sun Nov 28, 2004 4:33 am
by redmonkey
An alternative....
Code: Select all
<?php
$VIN='2GH445GH52W334556';
function str2array($string)
{
return explode("\x06", wordwrap($string, 1, "\x06", true));
}
print_r(str2array($VIN));
?>
...should output...
Code: Select all
Array
(
ї0] => 2
ї1] => G
ї2] => H
ї3] => 4
ї4] => 4
ї5] => 5
ї6] => G
ї7] => H
ї8] => 5
ї9] => 2
ї10] => W
ї11] => 3
ї12] => 3
ї13] => 4
ї14] => 5
ї15] => 5
ї16] => 6
)
Posted: Sun Nov 28, 2004 6:39 pm
by bobbyaa
Ok, now that I have the array of characters, how would I best go about converting the numbers from strings to that numerical value as an integer. Then I want to take the characters that are letters and convert them to a specific integer value based on the letter it was? Should I use the eregi_replace() function?
Posted: Sun Nov 28, 2004 7:42 pm
by redmonkey
Depends, if you are doing this once in the script then perhaps a simple
foreach() would do e.g.....
Code: Select all
<?php
function str2array($string)
{
return explode("\x06", wordwrap($string, 1, "\x06", true));
}
$VIN='2GH445GH52W334556';
$array = str2array($VIN);
foreach ($array as $k => $v)
{
if (is_numeric($v))
{
$array[$k] = (int) $v;
}
else
{
// do whatever processing
// required on non-numerical
// chars
}
}
var_dump($array);
?>
Or if you are doing this multiple times then perhaps using
array_walk() would be better...
Code: Select all
<?php
function str2array($string)
{
return explode("\x06", wordwrap($string, 1, "\x06", true));
}
function convert(&$v, $k)
{
if (is_numeric($v))
{
$v = (int) $v;
}
else
{
// do whatever processing
// required on non-numerical
// chars
}
}
$VIN='2GH445GH52W334556';
$array = str2array($VIN);
array_walk($array, 'convert');
var_dump($array);
?>
Either way should yield the same results...
Code: Select all
array(17) {
ї0]=>
int(2)
ї1]=>
string(1) "G"
ї2]=>
string(1) "H"
ї3]=>
int(4)
ї4]=>
int(4)
ї5]=>
int(5)
ї6]=>
string(1) "G"
ї7]=>
string(1) "H"
ї8]=>
int(5)
ї9]=>
int(2)
ї10]=>
string(1) "W"
ї11]=>
int(3)
ї12]=>
int(3)
ї13]=>
int(4)
ї14]=>
int(5)
ї15]=>
int(5)
ї16]=>
int(6)
}