I need to add itens from a text file, into a MySQL database, to do that i'm trying to use PHP to read the file and insert everything into the database, the problem is that the text file, has 2 itens per line, divided by spaces, something like this:
teste.txt
ITEM1 ITEM2
ITEM323 ITEM4
ITEM54 ITEM6
ITEM34234 ITEM8
I'm trying to remove the spaces using explode, but because the number of spaces, is random, i cannot do that. This is my Code:
$handle = #fopen("teste.txt", "r"); //read line one by one
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
list($a,$b)=explode(" ",$buffer);//Separate string by Spaces
//values.=($a,$b);// save values and use insert query at last or
echo $a;
echo "<br>";
echo $b . "<br>"; // NEVER echoes anything
}
What should i do?
You can insert a method to change multiple spaces to one:
$buffer = preg_replace('/\s+/',' ',$buffer);
Right before exploding it:
$buffer = fgets($handle, 4096); // Read a line.
$buffer = preg_replace('/\s+/',' ',$buffer);
list($a,$b)=explode(" ",$buffer);//Separate string by Spaces
You can use preg_replace() to perform a regular expression:
$buffer = preg_replace("/\ +/", " ", fgets($handle, 4096));
Related
i'm trying to make visitors count without sql with simple php code
counting the date list
i have log file called visits.log contains
2018-06-28
2018-06-28
only two lines
when i try to echo the lines gives me 5 i donno how but when i try to replace the log with something like
test
test
gives me 2 visitors
note: the text file doesn't contain empty space lines.
my php code
<p class="">Views today</p>
<span class=""><?php
$file="../../visitors.log";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo $linecount-1;
?></span>
my adding lines code
$line = date('Y-m-d') . " - $_SERVER[REMOTE_ADDR]";
file_put_contents('visitors.log', $line . PHP_EOL, FILE_APPEND);
Try the below code to get number of lines
$no_of_lines = count(file($file));
thanks for help guys my script was adding a hidden empty lines and i deal with it with replace()
replacing the empty lines to remove it
I started learning PHP this week and need to add a comma to the end of every line in a text file. I don't really know where to start. What would I do after I load the file? I can't find any tutorial saying how to write to the end of every line only the beginning of the file or end?? Is it possible to specify the end of a every line?
What I have so far:
fopen("file.txt",a)
????
I'm using "a" because I want to preserve the data.
This should work for you:
(Here I just get all file lines with file(). Then I go through each line with array_map() and concatenate a comma at the end. After this I save the file with the new content with file_put_contents())
<?php
$lines = file("test.txt", FILE_IGNORE_NEW_LINES);
$lines = array_map(function($v){return $v . "," . PHP_EOL;}, $lines);
file_put_contents("test.txt", $lines);
?>
Example file:
a
a
a
after the script:
a,
a,
a,
EDIT:
If you don't want to add a comma to an empty line just add a condition to check if the line is empty or not like this:
<?php
$lines = file("test.txt", FILE_IGNORE_NEW_LINES);
$lines = array_map(function($v){return $v . (empty($v)?"":",") . PHP_EOL;}, $lines);
file_put_contents("test.txt", $lines);
?>
Do you want to replace all the EOL with some specific character ?
$EOLChar=",";
$myFile = fopen("file.txt", "r") ;
$newFile="";
while(!feof($myFile)) {
$newFile.= str_replace(PHP_EOL, $EOLChar, fgets($myFile));
}
fclose($myFile);
file_put_contents("file.txt", $newFile);
I'm reading content from a text file which I'm showing on a page later, this is my code:
$lines = file("content.txt");
$i=1;
foreach($lines as $line ){
$var["line" . $i] = $line;
$i++;
}
extract($var);
The text file includes content in this format:
bla1
bla2
and so on, there's no space behind the domains just a linebreak, now I want to concatenate the content and show it, so I do this:
$as1 = $line1.$line2;
echo $as1;
But instead of the expected result
Bla1Bla2
I get
Bla1 Bla2
What am I doing wrong ? I can assure that there's no space in the text file not behind nor infront of the content.
There's no space; but unless you tell the file() function otherwise, there is a line feed at the end of each line
$lines = file("content.txt", FILE_IGNORE_NEW_LINES);
A browser will render a line feed as a space, unless inside a or block
use trim for this situation
$as1 = trim($line1).trim($line2);
echo $as1;
You could try trimming your input...
$lines = file("content.txt");
$i=1;
foreach($lines as $line ){
$var["line" . $i] = trim($line);
$i++;
}
extract($var);
I am looking for a suggestion on this:
I have a text file called movies.txt with about 900 lines, which contains one movie name per line. However, I would like to remove the year the movie has been released using PHP (which I am new to)
The format is basically:
A Nous la Liberte (1932)
About Schmidt (2002)
Absence of Malice (1981)
Adam's Rib (1949)
Adaptation (2002)
The Adjuster (1991)
The Adventures of Robin Hood (1938)
Affliction (1998)
The African Queen (1952)
So I am looking for a way to open the text file, reading it line by line and removing the (YEAR) values while also removing the space before the (YEAR).
Then I would like to save it as newmovies.txt
Would be great if you could show and explain me a solution that works for my needs. I am still very new to PHP (started a week ago) so it's all still magic to me.
You can read a file line-wise using the file() function. Then foreach over that and strip lines until the opening parenthesis.
For example
foreach (file($fn) as $line) {
$output[] = strtok($line, "(");
}
You may need to trim the extra space and add linebreaks again.
So a regex might be simpler and also asserts some structure without blindly cutting things off:
$text = file_get_contents($fn);
$text = preg_replace('/\s*\(\d+\)/m', '', $text);
# \s* is for spaces and \d+ is a placeholder for numbers
Then save that back.
<?php
$toWrite = "";
$handle = #fopen("input.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$toWrite .= preg_replace('/\(\d+\)/', '', $buffer) . "\n";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
file_put_contents("input.txt", $toWrite);
}
?>
I'm using PHP and I want to upload a text file, with the output / view showing each line from the text file on a new line (so exactly as it is displayed in the text file).
However, I also want the text file to be sorted alphabetically.
I have working code that uploads the file on each new line, and sorts by upper case, then lowercase - using the sort function
And I have code which sorts it alphabetically (regardless of case), but unfortunately the lines are grouped together, and not separated as I want them to be. - using the natcasesort
I've tried numerous things but not getting anywhere, so hoping someone can help either put the two together, or let me know what I need to do to either piece of code which will make each line show on a new line.
1st CODE NEEDS TO BE SORTED ALPHABETICALLY REGARDLESS OF UPPER/LOWERCASE
2nd CODE NEEDS TO SHOW THE LINE BREAKS
<?php
$file = file("users.txt");
sort($file);
for($i=0; $i<count($file); $i++)
{
$states = explode(",", $file[$i]);
echo $states[0], $states[1],"<br />";
}
?>
<?php
$filename="users.txt";
$lines = array();
$file = fopen($filename, "r");
while(!feof($file)) {
$lines[] = fgets($file,4096);
}
natcasesort($lines);
print_r($lines);
fclose ($file);
?>
You must use
$text = implode("<br />", $lines);
echo $text
It would glue the array and pasting a <br /> in every new line. Of course, if it is being printed in HTML, if not use the carrier \n instead.
And your code would look like:
.
.
.
while(!feof($file)) {
$lines[] = fgets($file,4096);
}
natcasesort($lines);
$text = implode("<br />", $lines);
print_r($text)
.
.
.