Getting variables from text file values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

I have a text file that has sections of any number of lines split with a double space \n\n\n

so it goes

Code: Select all

name phone number
descriptions shdfhdfhdf
fdghdhfdhdfghdh
fdhdfhdghdhdhdhfghjj
dfjfdjdfjdjdjd


name phone number
descriptions shdfhdfhdf
fdghdhfdhdfghdh


name phone number
descriptions shdfhdfhdf
fdghdhfdhdfghdhhdfhdf
fdghdhfdhdfghdhhdfhdf
fdghdhfdhdfghdhhdfhdf
fdghdhfdhdfghdhhdfhdf
fdghdhfdhdfghdhhdfhdf
fdghdhfdhdfghdhhdfhdf
fdghdhfdhdfghdhhdfhdf
fdghdhfdhdfghdh


name 724 838 7526
blah

How do i split this mess up into variable so I can get
$NAME, $PH ONE, $DESCRIPTION
$NAME, $PH ONE, $DESCRIPTION
$NAME, $PH ONE, $DESCRIPTION
$NAME, $PH ONE, $DESCRIPTION



I am at the end of my rope, I need to get btter with expressions i assume ;)
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

I am trying this... logic is there.. actual crap not working

Code: Select all

<?
$file = file("test1.txt"); 
$file = explode("\n\n", $file);
while(list(,$value)=each($file)) &#123;
list($line1,$line2)=explode("\n", $value);
list($1a, $1b)=explode(" ", $line1);

print("$line1 $line2");
&#125;
?>
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

I'm more than willing to send exact txt file and code to anyone who can help... it's driving me insane..

a problem (not the only problem)
is that NAME is one word where NUMBER is a ton of words or strings. then a /n the DESCRIPTS (many lines and words)
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

bump

Post by romeo »

bumpidee bump bump bumpidee bump bump lok at frosty go
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$file = file("test1.txt");
the contents of the file is already split into single lines. Try http://www.php.net/manual/de/function.fread.php instead.
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

THANK YOU SO MUCH FOR REPLYING

its still not working tho... gettinga
" Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /home/httpd/vhosts/h.com/httpdocs/images/conv.phtml on line 11 "

this is what i got, any help?


Code: Select all

<?
// get contents of a file into a string
$filename = "text1.txt";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));


$file = explode("\n\n", $contents);
while(list(,$value)=each($file)) 
&#123;
list($line1,$line2)=explode("\n", $value);
list($1a, $1b)=explode(" ", $line1);

print("$line1 $line2");
&#125;

fclose ($handle);
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$1a, $1b
variable-names must not begin with a number
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

LOFL, Im an idiot! Please don't agree publicly ;)


Im having aa problem tho.. I want line was to read the first word and then feed the rest of the line into the seocnd variable, right now im SPLIT at the " " but the rest of the line has spaces also... anyway to only split at the first " "
Post Reply