Parsing remote data - Need help


#1

I am accessing a php page that I append a username to and I receive back a text file containing data such as this. I always get 2 lines of data, and there are always 7 fields.

useridfirstnamelastnamemisc1misc2misc3misc4
JR007JessicaRobertsonaccountingArea55204

I really only need to keep the second line of data, ie: skip the first line, but I need to parse it out into 7 strings, so I can print only the strings I need. I am pretty new to php, or any language as far as that goes. Can someone give me a clue here?

I have tried using fopen and include to be able to grab the data, but the backslashes are killing me. I can’t get anything to parse correctly.


#2

I would look in to using the following functions:

strpos()
substr()

Links on how to use them:

http://www.php.net/strpos
http://www.php.net/substr

These should make it quite simple to get the job done. :)


#3

or explode()

Reference:
http://www.php.net/manual/en/function.explode.php


#4

$data=“useridfirstnamelastnamemisc1misc2misc3misc4”;
$pieces = explode("", $data);
echo $pieces[0];
echo $pieces[1];

Doesn’t work.


#5

Just wondering, but why doesn’t explode() work? Also if that doesn’t did you try using what I gave you?


#6

Can you give me an example using :

strpos()
substr()


#7

explode should work (and it’s the easiest way) - what results did you get that were invalid?

the other way is something like

variable to hold offset
loop    
    array[element] = substr($string, strpos($string, $needle, $offset))
    add the current strings lenght the the offset variable
end loop

or you could do a regrex - which would be faster but you have to know how to do it.

Again explode is the easiest way.


#8

The explode code should work fine, perhaps it’s something with the backslashes, like they are being escaped/not being escaped, or something? That’s the only thing I can think of…


#9

Well, I would imagine in the code example above, this line is the problem.

$data="useridfirstnamelastnamemisc1misc2misc3misc4";

The closing quote is being escaped, which no doubt will result in a parse error.

Does the data have to use backslashes? Try using | instead.