Page 1 of 1

[SOLVED] write in text file

Posted: Mon Aug 16, 2004 3:41 pm
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]

Posted: Mon Aug 16, 2004 5:01 pm
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?

Posted: Mon Aug 16, 2004 7:07 pm
by rcmn
yes it's exactly that !

Posted: Mon Aug 16, 2004 7:42 pm
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);

 }
?>

Posted: Mon Aug 16, 2004 8:13 pm
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);

?>

Posted: Mon Aug 16, 2004 8:36 pm
by rcmn
thank you.it's just perfect ....