[SOLVED] write in text file

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
rcmn
Forum Newbie
Posts: 21
Joined: Tue Jul 06, 2004 10:35 am

[SOLVED] write in text file

Post by rcmn »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color] 


i want be able to write what i can print_r . But i can't figured out how to do it.

Code: Select all

<?

 $handle = fopen("wdt_list.csv", "r");
while (!feof($handle)) {
   $node_id = fscanf($handle, "%[W0-9]   %[A-Za-z]\r\n"); //*find node id
   if ($node_id)
   {
     list ($serv_id) = $node_id;
     $serv_id =  substr($serv_id, 2, -2);    //*format to xxxx serv. format
     $serv_tab[] = $serv_id; //*create array
     $serv_unk = array_unique ($serv_tab); //* del duplicate
     $serv_lst = array_values($serv_unk);
     }
     $serv_id=NULL;
   }
fclose($handle);

   $count = count($serv_lst);
	for ($i = 0; $i < $count; $i++)
	
{

echo "<pre>";
print_r($serv_lst[$i]);
echo "</pre>";
 }
	
	
	$fd = 'lstserv.txt';
	$text = ???????;//$text insert in text file
	


if (is_writable($fd)) {// check if text writable

        if (!$handle = fopen($fd, 'w+')) {//w+ size to 0 and read write
         echo "can't open ($fd)";
         exit;
    }

    
    if (fwrite($handle, $text) === FALSE) {// write in the file
       echo "can't write ($fd)";
       exit;
    }

    echo " success;can write($text) in ($fd) ";

    fclose($handle);

} else {
    echo "the file $fd can't be write.";
}
note : print_r($serv_lst[$i]);
echo "</pre>";

give :

Code: Select all

1046
1041
1018
1015
1013
1011

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Last edited by rcmn on Mon Aug 16, 2004 8:39 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

So you want to write an array to a textfile and have it appear just like it would if you output:

Code: Select all

echo "<pre>";
print_r($my_array);
echo "</pre>";
?? Is that right?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
rcmn
Forum Newbie
Posts: 21
Joined: Tue Jul 06, 2004 10:35 am

Post by rcmn »

yes it's exactly that !
rcmn
Forum Newbie
Posts: 21
Joined: Tue Jul 06, 2004 10:35 am

Post by rcmn »

Now I 'm able to write in a text file but it write only the last ligne of the list . The number 1011 .

Code: Select all

<?php
  
 $handle = fopen("wdt_list.csv", "r");
while (!feof($handle)) {
   $node_id = fscanf($handle, "%[W0-9]   %[A-Za-z]\r\n"); //*find node id
   if ($node_id)
   {
     list ($serv_id) = $node_id;
     $serv_id =  substr($serv_id, 2, -2);    //*format to xxxx serv. format
     $serv_tab[] = $serv_id; //*create array
     $serv_unk = array_unique ($serv_tab); //* del duplicate
     $serv_lst = array_values($serv_unk);
     }
     $serv_id=NULL;
   }
fclose($handle);

   $count = count($serv_lst);
	for ($i = 0; $i < $count; $i++)
	
{
ob_start();

echo "<pre>";
print_r($serv_lst[$i]);
echo "</pre>";

$data = ob_get_contents(); // get print_r
ob_end_clean();

echo $data;

$fp = fopen('lstserv.txt', 'w+');
fputs($fp, $data); // write txt
fclose($fp);

 }
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php
 
$handle = fopen("wdt_list.csv", "r");
while (!feof($handle)) {
   $node_id = fscanf($handle, "%[W0-9]   %[A-Za-z]\r\n"); //*find node id
   if ($node_id)
   {
     list ($serv_id) = $node_id;
     $serv_id =  substr($serv_id, 2, -2);    //*format to xxxx serv. format
     $serv_tab[] = $serv_id; //*create array
     $serv_unk = array_unique ($serv_tab); //* del duplicate
     $serv_lst = array_values($serv_unk);
     }
     $serv_id=NULL;
   }
fclose($handle);

   $count = count($serv_lst);
   for ($i = 0; $i < $count; $i++)
   
{
  $data[] = $serv_lst[$i];
}

$data = trim(implode("\n",$data));
echo $data;

$fp = fopen('lstserv.txt', 'w+');
fputs($fp, $data); // write txt
fclose($fp);

?>
rcmn
Forum Newbie
Posts: 21
Joined: Tue Jul 06, 2004 10:35 am

Post by rcmn »

thank you.it's just perfect ....
Post Reply