when i try to insert each line into my oracle database i get an error stating invalid number, but if have only one line in the file it works fine.
$file = #fopen('file.text', "r") ;
// while there is another line to read in the file
while (!feof($file))
{
// Get the current line that the file is reading
$currentLine = fgets($file) ;
$currentLine = explode(' ',$currentLine) ;
insert($currentLine) ;
}
fclose($file) ;
the lines look like this
1 4 100
1 4 101
Try this:
$currentLine = trim(fgets($file));
It's possibly failing on the newline/carriage-return at the end of the line.
If not, where is this insert() function defined? Build a debug section that echos or writes out the attempted queries so you can really see the issue.
Your explode() call is using 9 spaces as a separator, but your file only appears to have 5 spaces between each number. This means your $currentline be a single element array containing the original line, and not separate elements with a number in each.
Either change the number of spaces in the explode call, or change to
$currentLine = preg_split('/\s+/', $currentLine);
which will split on any number of sequential spaces.
I would double check that there is not a new line at the end of the file. Maybe even double check inside of php that the line read is not blank.
$lineCount=0;
// while there is another line to read in the file
while (!feof($file))
{
$lineCount++;
// Get the current line that the file is reading
$currentLine = fgets($file);
if(trim($currentLine) != ''){
$currentLine = explode(' ',$currentLine) ;
insert($currentLine) ;
echo "Inserted line number $lineCount<br />";
} else {
echo "There was a blank line at line number $lineCount.<br />";
}
}
$lines = explode("\n", str_replace("\r", "", file_get_contents("file.text")));
foreach ($lines as $line)
{
insert(explode(' ', $line);
}
try this :
// Read all data in the file
$file_all = file_get_contents('file.text') or die("unable to open file");
// create an array with each index = a line
$file_lines = explode(chr(13),$file_all);
// Insert each line found
foreach ($file_lines as $file_line) { insert($file_line); };
Related
I have a big text file, split by new lines and on every line elements split by ';', like this:
1;1;20;3.6;0%;70%;25%;0%;5%;
1;2;80;4;45%;20%;20%;15%;0%;
1;3;80;4;40%;35%;5%;20%;0%;
1;4;20;3.6;15%;40%;38%;5%;2%;
1;5;20;3.6;30%;18%;33%;20%;0%;
1;6;80;4;27%;47%;23%;3%;0%;
What I would like to do is with PHP is to read the file correctly and access a specific element in any row, for example on row 2, element 3 (maybe, [1][2], if considered as indexes) and print it.
<?php
//split by new line
$text = fopen("public/data/data1.txt", "r");
if ($text) {
while (($lines = fgets($text)) !== false) {
//split by ;
$line = explode(';', $lines);
//access a specific element
}
fclose($text);
} else {
// error opening the file.
}
?>
Does somebody know how I could access this elements?
You can explode the string twice.
First on lines, then on ;.
$arr = explode(PHP_EOL, $str);
Foreach($arr as &$line){
$line = explode(";", $line);
}
https://3v4l.org/5fbvZ
Then echo $arr[1][2]; will work as you wanted
I'm trying to figure out how to skip the first and last line of a text file when I'm reading it while using fgets(). The first line could be solved with an if(!$firstLine), but I'm not sure how to ignore the last line or if my solution for ignoring the first line is the best choice.
fgets($file); //Ignore the first line
$line = fgets($file);
$next = fgets($file);
while ($next !== false) { //check the line after the one you will process next.
//This way, when $next is false, then you still have one line left you could process, the last line.
//Do Stuff
$line = $next;
$next = fgets($file);
}
$lines = file('http://www.example.com/');//or local file
unset($lines[0]);
unset($lines[count($lines)-1)]);
$new_file = implode('', $lines); //may want to use line break "\n" for the join
You might want to try exploding the file contents. It will help you alot! After that you will label it.
Unfortunately, file() will open file into "serial" stream (not sure if serial is right word), it means that you must read full file before detecting his end
try this
$str= file_get_contents("file.txt");
$arr = preg_split ("\n",$str);
$n = count($arr);
$n is a number of lines
$str="";
for($i=0;$i<$n;$i++){
if($i>0 && $i<($n-1)){ $str.=$arr[$i]}
}
/**Str without line 1 and final line */
I need to insert a line in a textfile between two other lines:
1: 30-12:32 1
2: 30-12:34 1
3: 30-12:35 1
For an example I need to insert a line between 1 and 2 which says 30-12:34 0.
I have the code to calculate what needs to be inserted by looking at the previous number but I need help inserting the text.
Thank you
Use this and then
function replaceInFile($what, $with, $file){
$buffer = "";
$fp = file($file);
foreach($fp as $line){
$buffer .= preg_replace("|".$what."[A-Za-z_.]*|", $what.$with, $line);
}
fclose($fp);
echo $buffer;
file_put_contents($file, $buffer);
}
and then enter what as the line just before it in text and with as what you want after it.
I am trying to read a file one word at a time. So far I have been able to use fgets() to read line by line or up to a certain amount of bytes, but that is not what I am looking for. I want one word at a time. up to the next white space, \n, or EOF.
Does anyone know how to do this in php. In c++ I just use the 'cin >> var' command.
you can do this by
$filecontents = file_get_contents('words.txt');
$words = preg_split('/[\s]+/', $filecontents, -1, PREG_SPLIT_NO_EMPTY);
print_r($words);
this will give you array of words
For some replies in this topic: I say this: Do not reinvent the wheel.
In PHP use:
str_word_count ( string $string [, int $format [, string $charlist ]] )
format:
0 = Return only the number of words;
1 = Return an array;
2 = Return an associative array;
charlist:
Charlist are characters which you consider a word.
Function.str-word-count.php
[CAUTION]
Nobody know anything about the size of your file content, if your file contents is big, exists many flexible solutions.
(^‿◕)
You would have to use fgetc to get a letter at a time until you hit a word bountry then do something with the word. Example
$fp = fopen("file.txt", "r");
$wordBoundries = array("\n"," ");
$wordBuffer = "";
while ($c = fgetc($fp)){
if (in_array($c, $wordBountries)){
// do something then clear the buffer
doSomethingWithBuffer($wordBuffer);
$wordBuffer = "";
} else {
// add the letter to the buffer
$wordBuffer.= $c;
}
}
fclose($fp);
You can try fget() function which read file line by line and when you get one line from file you use explode() to extract word from line which separated by space.
Try this code:
$handle = fopen("inputfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
$word_arr = explode(" ", $line); //return word array
foreach($word_arr as $word){
echo $word; // required output
}
}
fclose($handle);
} else {
// error while opening file.
echo "error";
}
I'm trying to figure out how to skip the first and last line of a text file when I'm reading it while using fgets(). The first line could be solved with an if(!$firstLine), but I'm not sure how to ignore the last line or if my solution for ignoring the first line is the best choice.
fgets($file); //Ignore the first line
$line = fgets($file);
$next = fgets($file);
while ($next !== false) { //check the line after the one you will process next.
//This way, when $next is false, then you still have one line left you could process, the last line.
//Do Stuff
$line = $next;
$next = fgets($file);
}
$lines = file('http://www.example.com/');//or local file
unset($lines[0]);
unset($lines[count($lines)-1)]);
$new_file = implode('', $lines); //may want to use line break "\n" for the join
You might want to try exploding the file contents. It will help you alot! After that you will label it.
Unfortunately, file() will open file into "serial" stream (not sure if serial is right word), it means that you must read full file before detecting his end
try this
$str= file_get_contents("file.txt");
$arr = preg_split ("\n",$str);
$n = count($arr);
$n is a number of lines
$str="";
for($i=0;$i<$n;$i++){
if($i>0 && $i<($n-1)){ $str.=$arr[$i]}
}
/**Str without line 1 and final line */