faqts : Computers : Programming : Languages : PHP : Common Problems : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes


Need to take from a cut&paste"Tanks 20 Planes 32" etc&place them into own arrays-$array["Tanks"] etc

Apr 17th, 2002 13:57
Techek, Mike Butler,

I'd use a regular expression to do the job.
Example :
// this is your input
$input = "Tanks 20 Planes 32";
// match one or more chars, a space and the one or more numbers and 
store each match in an array (matches)
$ok = eregi("^([a-zA-Z0-9]+ [0-9]+)*$", $input, $matches);
// iterate through the matches
while(list($index, $value) = each($matches)) {
  // slice the value into a type and a size
  list($type, $size) = explode(' ', $value);
  // make the array with type as index and size as value
  $ressources[$type] = $size;
  // display the results
  echo "$type : $size<br>\n";
The regex is the weakest point as I'm not currently able to test it, 
but it should work if I'm not mistaken!
Give me a call if you need a WORKING example *s*