Ruth
Ruth

Reputation: 355

PHP explode a txt file

I have the following txt file:

++Marry++M++B++NY++

++Jack++F++O++LS++

write to a .txt file:

Name:Marry

Sex:M

Blood type:B

City:NY

Name:Jack

Sex:F

Blood type:O

City:LS

My code is:

$fn = fopen("test.txt","r") or die("fail to open file");
 
$content = array();
 
 while($row = fgets($fn)) {

    $num = explode("++", $row);
    $name = $num[0];
    $sex = $num[1];
    $blood =  $num[2];
    $city = $num[3];
 }

$fp = fopen('output.txt', 'w');     
fwrite($fp, $row);
fclose($fp);

Should this work? Because it returns nothing.

Thx.

Upvotes: 0

Views: 18136

Answers (5)

Vitalicus
Vitalicus

Reputation: 1379

use file_get_contents()

$row=file_get_contents("test.txt");
$list=explode("++",$row);    
echo 'Name:'.$list[0].'<br>';
echo 'Sex:'.$list[1].'<br>';

Upvotes: 0

Andrea Gorrieri
Andrea Gorrieri

Reputation: 1772

I had a similar problem, I wasn't able to print the [1] component of the "explode" result while [0] component printed correctly. In my case I solved changing the while loop from:

while(!feof($myfile)) {

        $row = fgets($myfile);

        $components = explode(",", $row);
        $myVar = $components[1];
        echo $myVar;
}

to

while($row = fgets($myfile)) {

        $components = explode(",", $row);
        $myVar = $components[1];
        echo $myVar;
}

Upvotes: 0

Niclas Larsson
Niclas Larsson

Reputation: 1317

$fn = fopen("test.txt","r") or die("fail to open file");

while($row = fgets($fn)) {
  list( $sName, $sSex, $sBlood, $sCity ) = explode( "++", $row );

  echo 'Name:' . $sName . '<br />';
  echo 'Sex:' . $sSex . '<br />';
  echo 'Blood type:' . $sBlood . '<br />';
  echo 'City:' . $sCity . '<br />';
}

fclose( $fn );

To write to a file you must fist create a buffer and then write to the file. The easiest way to do this would be with file_put_contents. Be aware of that the file_put_content method uses more memory then fopen, fwrite and fclose does.

$fn = fopen("test.txt","r") or die("fail to open file");

$sBuffer = '';
while($row = fgets($fn)) {
    list( $sName, $sSex, $sBlood, $sCity ) = explode( "++", $row );

    $sBuffer .= 'Name:' . $sName . PHP_EOL;
    $sBuffer .= 'Sex:' . $sSex . PHP_EOL;
    $sBuffer .= 'Blood type:' . $sBlood . PHP_EOL;
    $sBuffer .= 'City:' . $sCity . PHP_EOL;

    $sBuffer .= PHP_EOL; // There will be a empty line after each "set"
}

fclose( $fn );

file_put_contents( 'path/to/file.txt', $sBuffer );

With fopen, fwrite and fopen.

$fn = fopen("test.txt","r") or die("fail to open file");

$rWrite = fopen( 'path/to/file.txt', 'w' ) or die( 'Could not open file for writing' );

while($row = fgets($fn)) {
    list( $sName, $sSex, $sBlood, $sCity ) = explode( "++", $row );

    fwrite( $rWrite, 'Name:' . $sName . PHP_EOL );
    fwrite( $rWrite, 'Sex:' . $sSex . PHP_EOL );
    fwrite( $rWrite, 'Blood type:' . $sBlood . PHP_EOL );
    fwrite( $rWrite, 'City:' . $sCity . PHP_EOL );

    fwrite( $rWrite, PHP_EOL ); // There will be a empty line after each "set"
}

fclose( $fn );
fclose( $rWrite );

Upvotes: 4

ace
ace

Reputation: 7583

You are almost in the right direction. You need to include your write to file in the same loop that you are getting your contents.

$fn = fopen("test.txt","r") or die("fail to open file");

$fp = fopen('output.txt', 'w') or die('fail to open output file');
while($row = fgets($fn)) {
  $num = explode("++", $row);
  $name = $num[0];
  $sex = $num[1];
  $blood =  $num[2];
  $city = $num[3];

  fwrite($fp, "Name:$name\n");
  fwrite($fp, "Sex:$sex\n");
  fwrite($fp, "Blood:$blood\n");
  fwrite($fp, "City:$city\n");
}
fclose($fn);
fclose($fp);

Your initial code will not work because fwrite expects a string and you are passing an array. And in the case that you did pass the string, it will only include the last line of your test.txt file.

Upvotes: 0

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

It doesn't output anything because you are arn't using $name, $sex etc. Try to change the following snippet in your code and see what it does now:

while($row = fgets($fn))
{

    $num = explode("++", $row);
    $name = $num[0];
    $sex = $num[1];
    $blood =  $num[2];
    $city = $num[3];

    echo "$name $sex $blood $city<br>";
 }

Upvotes: 1

Related Questions