I have a text file with line breaks that I have already imploded in to single line and added commas to the end of every line.
I need to remove every 36th comma from the line and add line break as well.
this is my code:
$lines = file("filename.txt", FILE_IGNORE_NEW_LINES);
$comma_separated = implode(",", $lines);
$lines2 = preg_replace('/(?:[^,]*,){36}/', '$0\r\n', $comma_separated);
I have always had trouble with coding. My brains just don't go that way. I try my best, and I am 100 % sure I have searched and read the solution, but I have not understood it.
I would rather do something like this.
Instead of a regex that will eventually fail, add a string to every 36 line before you implode.
This string can easily be found with a simple str_replace() to remove and add the line break.
$lines = file("filename.txt", FILE_IGNORE_NEW_LINES);
For($i=0; $i<count($lines);){
$lines[$i] .= "something";
$i = $i+36;
}
$comma_separated = implode(",", $lines);
// String replace something to line break.
$result = str_replace("something,", "<br>", $comma_separated);
Edit sorry the string replace of course needs to go after the implode.
Sorry for the confusion.
$lines = file("filename.txt", FILE_IGNORE_NEW_LINES);
For($i=0; $i<count($lines);){
//your code
if($i%36==0){
trim($line,',');
}
}
Related
I apologize for a confusing title. Basically, i'm facing a problem with my website which ends up bugging it completely. I need to basically remove all duplicate entries on the same line, on all lines in my text file list. I.e.
123123
123
123
Sometimes i get entries like 123123 on the same line when it should just be 123, on each line. This is just an example of course, it's hard for me to explain. I apologize again. I hope this was enough for you to gasp what i mean.
To sum it up, i'm need to remove the duplicate part of the string 123123, so it's just 123, for all of the lines in my text file.
Help appreciated.
A live example for this:
2017-06-21:127.0.0.12017-06-21:127.0.0.1
2017-06-21:127.0.0.12017-06-21:127.0.0.1
2017-06-21:127.0.0.12017-06-21:127.0.0.1
2017-06-21:127.0.0.1
$linesStr = '2017-06-21:127.0.0.12017-06-21:127.0.0.1
2017-06-21:127.0.0.12017-06-21:127.0.0.1
2017-06-21:127.0.0.12017-06-21:127.0.0.1
2017-06-21:127.0.0.1';
//can be \n only
$lines = explode("\r\n", $linesStr);
//loop through all lines
foreach($lines as $i => $line)
{
$lineLen = ceil(strlen($line) / 2);
$first = substr($line, 0, $lineLen);
$second = substr($line, $lineLen);
if($first == $second)
{
$lines[$i] = $first;
}
}
$lines = implode("\r\n", $lines);
This should do it...
A basic algorithm to deduplicate a string:
Split it in half.
If the both halves are the same, replace the whole string with either half.
Caveat: this doesn't care whether or not the string was intended to be a duplicate or not, and consequently may remove some things you don't want it to.
function deduplicate($str) {
$str = trim($str);
list($beginning, $end) = str_split($str, strlen($str) / 2);
return ($beginning == $end) ? $end : $str;
}
Assuming you have an array of lines from your file you can apply it with array_map.
$lines = array_map('deduplicate', $lines);
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 am trying to read a line where multiple numbers were separated by space using php. Initially I tried using fscanf however the issue is, as fscanf read one line at a time, it only reads the first number. I am confused what to do.
Sample Input
20 30 -5987 456 523
The best approach for this case is to use a combination of explode and file reading. The strategy is initially read the whole line as an string. Then use explode to store the all the number in an array. However in that case the array would a string array later on we can change the type of array element from String to integer. Here is how
<?php
$_fp = fopen("php://stdin", "r");
fscanf($_fp, "%d\n", $count);
$numbers = explode(" ", trim(fgets($_fp)));
foreach ($numbers as &$number)
{
$number = intval($number);
}
sort($numbers);
?>
$input = trim(fgets(STDIN));
$arr = explode(" ", $input);
foreach($arr as &$number){
$number = (int)$number;
}
If you want to eliminate white space from "fopen" function user "trim" function or surround variable with trim function.
Example :
echo "Please enter series limit : ";
$handles = fopen ("php://stdin","r");
$n = trim(fgets($handles));
So here we can remove white space in between the characters as well as at the end.
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 */
*strong text*what i want to do is from my breakline content to one line paragraph
example my var_dump result:
string(212) "(73857,"2012-02-02 03:18:44","TXT",60143836234);"
string(122) "(73858,"2012-02-02 03:20:08","WAP",60143836234);"
string(211) "(73859,"2012-02-02 08:21:47","TXT",60163348563,);"
What i want to become:
string(555) "(73857,"2012-02-02 03:18:44","TXT",60143836234);(73858,"2012-02-02 03:20:08","WAP",60143836234);(73859,"2012-02-02 08:21:47","TXT",60163348563,);"
update (here is my code, $i is the line break records, if I able to make the breakline to one line, i will put in a new file )
foreach($get_line_feed_content as $i) {
$add_special_char = "(".$i.");";
var_dump($add_special_char);
if(!empty($i)){
$stringData = $final_content;
fwrite($save, $stringData);
fclose($save);
}
}
any idea?
Thank and highly appreciated your answer
Did you just want to glue the strings together? I don't see any line breaks from your dumps.
If that's the case, just do:
$finalString = $string1 . $string2 . $string3;
var_dump($finalString); //Strings should be glued as one.
If, however, each line is represented as an element in an array:
$stringsArray = array('string1', 'string2', 'string3');
$finalString = implode("", $stringsArray);
var_dump($finalString);
With your most recent update, this is what I would do:
$newString = '';
foreach($get_line_feed_content as $i) {
$newString .= "(".$i.");"; //concatenate
var_dump($newString); //You will get a lot of dumps and with each dump, a new string should be appended to it.
if(!empty($i)){
fwrite($save, $newString);
fclose($save);
}
}
if you want to glue strings you can do like this:
$data = $string1.$string2.$string3;
If you have array of strings do like this:
$strings = array('text1','text2','text3');
$data = implode('', $strings);