views:

233

answers:

4

I have data which I wish to be pasted into a textbox, it will be in the form E.G

Ryu Aiter D78:21:87:13 177 /177 1 / 6
Ryu Chronos D78:21:26:21 182 /182 0 / 6
Ryu Hermes D78:21:26:22 201 /201 0 / 6
Ryu Hefaistos D78:31:75:10 136 /136 1 / 2
Ryu Krotos D78:84:96:11 170 /170 1 / 6
Ryu Heros D78:65:51:31 175 /175 2 / 5
Ryu Arachnos D78:13:84:11 185 /185 0 / 5

its splits up like this
Base(max 16 chars)
Location(staring D , 12 chars)
econ/max econ (int/int)
used/Total(int/int)

What i wish to do is create a loop for each Row of text, and then inside that loop chop out each part of the row into variables for each component.

as far as ideas on separating it i know that the : symbol is banned from names and bases. so if i find the first ":" then step back 2 and take the next 12 chars that is my location i know that can be done with a until loop and if(string[x]=':')

But how do i loops through rows? And how can i separate the rest of the data in a row?

A: 

Can you just use explode and gradually break it down?

eg.

  • explode the entry into seperate lines at '\n'
  • then explode each line into 2, everything before the 1st ':', and everything after
  • explode the 1st part into pieces using each 'space' to give you Name, Base, Location
  • explode the 2nd part using 'space', ':' or '/' to give you econ/max econ and used/Total
philm
+1  A: 

This is what regular expressions are for :P try this out:

$lines = explode( "\r\n", $data );

$users = array();

foreach( $lines as $line )
{
    $matches = array();
    $user = array();

    preg_match( "/([^ ]+) ([^ ]+) ((?:[A-Z])(?:[0-9]+:){3}[0-9]+) ([0-9]+) \/([0-9]+) ([0-9]+) \/ ([0-9]+)/", $line, $matches );

    list(,$user['name'],$user['base'],$user['location'],$user['econ'],$user['maxecon'],$user['used'],$user['total']) = $matches;

    $users[] = $user;
}

You will have an array called users which contains a series of associative arrays with the components. Like below...

Array
(
[0] => Array
    (
        [total] => 6
        [used] => 1
        [maxecon] => 177
        [econ] => 177
        [location] => D78:21:87:13
        [base] => Aiter
        [name] => Ryu
    )

[1] => Array
    (
        [total] => 6
        [used] => 0
        [maxecon] => 182
        [econ] => 182
        [location] => D78:21:26:21
        [base] => Chronos
        [name] => Ryu
    )

etc, etc...

EDIT: I made a lot of assumptions about the data as you haven't given many details, if you need further help with the expression let me know.

UPDATE AS PER YOUR COMMENT:

Line 182: $name = htmlspecialchars(mysql_real_escape_string($_POST['name']));
Line 188: $tecon = htmlspecialchars(mysql_real_escape_string($user['econ']));

You should turn on display_errors as they were simple syntax errors that could easily be debugged.

Kane Wallmann
You can shorten that by using the m modifier (http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php)
antennen
ok i used this and i would have thought it would all be nice, but somhow the page will not load, must be a mistake im my php..php as txt file:http://www.teamdelta.byethost12.com/dumproute.txtits probly a silly mistake.
Arthur
I've updated my answer with what you need to do to fix the file you uploaded.
Kane Wallmann
That works nicly now only one issue left and that is my falt, but i misunderstoof the data format, the first (max 16) chars eg(Ryu Aiter) is the base, name was to be given to me in a seperate form sorry,base name can have many spaces in it, and i need a new preg match pattern.
Arthur
preg_match( "/(.+?) ((?:[A-Z])(?:[0-9]+:){3}[0-9]+) ([0-9]+) \/([0-9]+) ([0-9]+) \/ ([0-9]+)/", $line, $matches );
Kane Wallmann
Thanks all working now :)
Arthur
A: 
if(preg_match_all('/^([a-z]{1,16})\s+([a-z]{1,16})\s+(D\d+):(\d+):(\d+):(\d+)\s+(\d+)\s+\/(\d+)\s+(\d+)\s+\/\s+(\d+)$/mi', $yourinputgoeshere, $match) {
    print_r($match);
}

This should, untested, get everything into a large array, separated. Untested

antennen
A: 

Extending the same idea i am trying to use preg match for a single line on another page.

i use the code

   $data = $_POST['list'];

   $matches = array();
   $user = array();   
   preg_match( "/(.+?) ((?:[A-Z])(?:[0-9]+:){3}[0-9]+) ([0-9]+) \/([0-9]+) ([0-9]+) \/ ([0-9]+)/", $data, $matches ); 
   list(,$user['base'],$user['location'],$user['econ'],$user['maxecon'],$user['used'],$user['total']) = $matches;

   $base = $users['base'];
   $location = $users['location'];
   $tecon = $users['econ'];

i used echo to print out £data and it contains the data as expected but the same code without the lines loop does not seperate the parts of my data into the array..in fact the array size of $user remains empty, what has gone wroung?

Arthur