1

I have this txt file structure:

"data";"data";"data";#;"my data";"my data";"my data"
"data";"data";"data";#;"my data";"my data";"my data"
"data";"data";"data";#;"my data";"my data";"my data"

I need to read this file data just after the # sign. My PHP code just for read the entire line.

$file_handle = fopen("texto.txt", "r");
$numlinha = 0;
while (!feof($file_handle)) {
   $line = fgets($file_handle);
   $numlinha++;
   echo $numlinha . ". " . $line . "</br></br>";
}
fclose($file_handle);
CMartins
  • 3,247
  • 5
  • 38
  • 53
  • 2
    possible duplicate of [Extracting data from a csv file using php](http://stackoverflow.com/questions/5968073/extracting-data-from-a-csv-file-using-php) – Gordon Jun 20 '11 at 15:19
  • 1
    possible duplicate of [How to extract data from a CSV file in PHP](http://stackoverflow.com/questions/2805427/how-to-extract-data-from-csv-file-in-php/2805486#2805486) – Gordon Jun 20 '11 at 15:20
  • 2
    And in case you really want the full text after the #, http://de3.php.net/manual/en/function.strstr.php – Gordon Jun 20 '11 at 15:27

4 Answers4

1

You can use strpos function to find position of first # char in your line.

$file_handle = fopen("texto.txt", "r");
$numlinha = 0;
while (!feof($file_handle)) {
   $line = fgets($file_handle);
   $cpos = strpos($line, '#');
   if ($cpos !== FALSE) {
       $line = substr($line, 0, $cpos);
   }
   $numlinha++;
   echo $numlinha . ". " . $line . "</br></br>";
}
fclose($file_handle);
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
  • Thanks to all. Very useful tips. One more thing, can I put each data value on a $_SESSION variable and give names like $_SESSION['peso'] for the first $_SESSION['clientID'] for the second...? – CMartins Jun 20 '11 at 15:57
1
$file_handle = fopen("texto.txt", "r");
$numlinha = 0;
while (!feof($file_handle)) {
   $line = fgets($file_handle);
   $parts = explode("#", $line);
   $parts[0] // part before the # in this line
   $parts[1] // part behind the # in this line
   $numlinha++;
   echo $numlinha . ". " . $line . "</br></br>";
}
fclose($file_handle);
DanielB
  • 19,910
  • 2
  • 44
  • 50
1

You could use: $data = explode(";#;", $line); And then do your processing on $data[1] instead of $line.

This assumes that ;#; is unique on each line...

Note that using string position testing (strpos()) and substr() to extract the part of the string would be more resource consuming, I believe, than just taking the line you already read and splitting it at the exact known delimiter, which in this case is ;#;.

The other examples posted assume that # will be only on the line once, or at least the divide will be the first # in the line. Using ;#; would make it more unique, and your result can be processed by the str_getcsv() function if you needed to break it down into an array of values for other uses.

rustyvz
  • 13
  • 1
  • 4
1

You can use the explode function, to split the string in two parts using the "#" delimiter

http://php.net/function.explode

Alex
  • 249
  • 1
  • 6