Web Hosting Talk







View Full Version : Processing plain text with php


harryhood
03-12-2004, 12:53 PM
I need to create a html form hosted on my web site. When the form is submitted it posts to a script (script A) hosted on another site. Script A takes the data posted from the form and does some processing, sorting and organizing of the information from the my form.

Then script A passes the "re-organized" data back to my form page in the following format as plain text:


1
STREET= my street
STREET= still my street
STREET=
L=some city
S=some State
PostalCode=
C=US
Email=admin@mydomain.com
Phone=



I'd like to be able to take the response above and assign the value from each line to a variable. For example:

$status = '1';
$street1 = 'my street';
$street2 = 'still my street';
$city = 'some city';
etc...

In theory I think I should be able to parse through the response and split the data at each line feed, then assign each portion to a variable.

That of course is only an idea, and I have no idea of how to actually do it.

Any ideas would be greatly appreciated.


Thanks,

nnormal
03-12-2004, 01:19 PM
you can use the explode function which searches a string for a character (or characters) and splits it into an array which you could then referance by the arrays element. I usually avoid exploding strings by special characters like a newline because different environments can treat them differently. The most common use is a comma but I tend to use 3 &'s like this...



// read a file called text which has items seperated by &&&
$filelocation = 'text.txt';
$newfile = fopen($filelocation,"r");
$content = fread($newfile, filesize($filelocation));
fclose($newfile);

// show a <p> inbetween all items
$items = explode("&&&",$content);
for ($i=0; $i<count($items); $i++) echo "$items[$i]<p>";

robeyh
03-13-2004, 02:09 AM
$ret; //variable passed back

$ret = str_replace("\r\n","\n",$ret);
$ret = str_replace("\r","\n",$ret);

$ret = explode("\n", $ret);

foreach($ret as $line){
if(!strstr($line,'=')){
$vals = explode('=',$line);
$$vals[0] = join(array_slice($vals,1));
}
}



That should do the job.