Forum Moderators: coopster

Message Too Old, No Replies

Separating Lines of a Text Field

         

inveni0

3:24 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



How would I go about splitting:

John Doe

Denver

Colorado

Into separate variables for each line so that I can plug them into a form?

coopster

3:27 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If they are in an array you can use a list [php.net]:
<?php 
$array = array('John Doe', 'Denver', 'Colorado');
list($name, $city, $state) = $array;
?>

inveni0

3:34 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



Actually, they'll come from a text area on a form. On submitting, the form will split each line into its variable. The data will always be the same type on each entry, so the code will not have to detect what is what.

inveni0

4:00 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



I've managed to convert the string to:

John Doe : Denver : Colorado

But I can't find a split function that will split it up. I've tried:

$new = preg_split(" : ", $str);

This returns an empty array.

inveni0

4:05 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



Go figure that a simple split(":", $str); would work!

Now one other question...

When using nl2br on my initial variable, I get two <br />'s in the variable (one for each carriage return). How can I turn the two of them together into one single ":"?

jatar_k

4:17 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you should get 2, in your example they are double spaced

you could explicitly replace <br /><br /> with :

inveni0

4:21 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



yes, but when I use:

$str = str_replace("<br /><br />", ":", $str);

It doesn't replace anything.

Here is exactly how I'm processing it:

<?php
$recordinfo = nl2br($recordinfo);
$recordinfo = str_replace("<br /><br />", ":", $recordinfo);
$newinfo = split(":", $recordinfo);

?>

inveni0

4:35 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



I got it to work. I had to add a line and separate ": :" into ": ".": "

<?php
$recordinfo = nl2br($recordinfo);
$recordinfo = str_replace("<br />", ":", $recordinfo);
$recordinfo = str_replace(": ".": ", ":", $recordinfo);
$newinfo = split(":", $recordinfo);

?>

jatar_k

4:38 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice

another thought is to go at it from another way, by replacing the \r\n or even splitting on \r\n

but you don't need to change what already works. ;)

inveni0

4:57 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



Actually, it didn't work.....

So I just use every other array entry. Instead of:

$newinfo[0]
$newinfo[1]

I use:

$newinfo[0]
$newinfo[2]

jatar_k

5:29 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could try collapsing it

<?php
$recordinfo = str_replace("\r", "", $recordinfo);
$recordinfo = str_replace("\n\n", "\n", $recordinfo);
$recordinfo = str_replace("\n", ":", $recordinfo);
$newinfo = split(":", $recordinfo);
?>

might work

inveni0

5:54 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



Yeah. I don't mind taking every other array variable, though.

So now my journey continues...

I've now created a PHP form that takes the text in a .txt file and turns it into a string. I can then process that string as I did the previous information.

My goal here is to have, say, 100 files with data like this stored in each. I'd like to have my PHP form check the directory where the file is stored, open the first file in the directory and use its data. The data would fill my form, so I could look it over and then submit it to a database.

How could I get PHP to grab the first file, read it, and delete it after its done so that after adding to the database, the form would reload with the NEW first file in the directory?

inveni0

6:23 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



I've decided to name my files consecutively. So here's my code:

<?php
//get first file
$count = $count + 1;
$firstfile = "test".$count.".txt";
// get contents of a file into a string
$filename = $firstfile;
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
//split string
$contents = nl2br($contents);
$contents = str_replace("<br />", ":", $contents);
$contents = str_replace(": ".": ", ":", $contents);
$newcontents = split(":", $contents);
//delete file
// unlink($filename);
?>

I've commented out the delete option because I'm not sure I want to use it yet.

Also, in my form I have a hidden field that contains the $count variable so that it passes through with each form submission.

Can anyone see any problems I may run into with this method?

jatar_k

6:27 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



looks ok, test it and keep your unlink commented

I was just writing an answer, I came up with something like this

<? 
$mydir = "/path/to/dir/";
$d = dir($mydir);
while($entry = $d->read()) {
if ($entry!= "." && $entry!= "..") {
$myfile = $mydir . $entry;
$fp = fopen($myfile, "r");
// do your importing of the file contents here
fclose($fp);
unlink($myfile);
}
}
$d->close();
?>

inveni0

6:38 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



Thanks for that answer!

My goal here is to scan paper documents and have image recognition software convert them into text files (only certain fields based on a template).

Then, I can upload these files to my webserver, where my PHP App will autoload each file into the record insertion form. This will allow easy conversion of paper documents to database.

My first solution works, but I will have to see how my scanner program names files before I'll know for sure. Dependant on that, I may have to use your idea.

Thanks for your help!