explode

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
bobbyaa
Forum Newbie
Posts: 17
Joined: Wed Oct 13, 2004 4:54 pm

explode

Post 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?
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post 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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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;
}


?>
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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
(
    &#1111;0] =&gt; 2
    &#1111;1] =&gt; G
    &#1111;2] =&gt; H
    &#1111;3] =&gt; 4
    &#1111;4] =&gt; 4
    &#1111;5] =&gt; 5
    &#1111;6] =&gt; G
    &#1111;7] =&gt; H
    &#1111;8] =&gt; 5
    &#1111;9] =&gt; 2
    &#1111;10] =&gt; W
    &#1111;11] =&gt; 3
    &#1111;12] =&gt; 3
    &#1111;13] =&gt; 4
    &#1111;14] =&gt; 5
    &#1111;15] =&gt; 5
    &#1111;16] =&gt; 6
)
bobbyaa
Forum Newbie
Posts: 17
Joined: Wed Oct 13, 2004 4:54 pm

Post 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?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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) &#123;
  &#1111;0]=&gt;
  int(2)
  &#1111;1]=&gt;
  string(1) "G"
  &#1111;2]=&gt;
  string(1) "H"
  &#1111;3]=&gt;
  int(4)
  &#1111;4]=&gt;
  int(4)
  &#1111;5]=&gt;
  int(5)
  &#1111;6]=&gt;
  string(1) "G"
  &#1111;7]=&gt;
  string(1) "H"
  &#1111;8]=&gt;
  int(5)
  &#1111;9]=&gt;
  int(2)
  &#1111;10]=&gt;
  string(1) "W"
  &#1111;11]=&gt;
  int(3)
  &#1111;12]=&gt;
  int(3)
  &#1111;13]=&gt;
  int(4)
  &#1111;14]=&gt;
  int(5)
  &#1111;15]=&gt;
  int(5)
  &#1111;16]=&gt;
  int(6)
&#125;
Post Reply