I want to organize the texts in a file so if the texts in two or more lines are same, I want to keep them together.
Example:
US
CA
US
Would change to:
US
US
CA
I looked around, but couldn't find an answer really.
I can get the file to open with this:
$file = fopen("file.txt", "r");
while(!feof($file)){
$line = fgets($file);
// do same stuff with the $line
}
fclose($file);
Any help would be appreciated!
$pieces = explode(" ", $line);
// sorts it alphabetically
sort($pieces, SORT_NATURAL | SORT_FLAG_CASE);
Sounds like it should do the trick.
Related
i have question, for get specific contain of file. i already get all data from many file .job extension, now i want to get data just in JobID rows so i can get all data JobID rows from many file .job, i want the output be "115518024" and more like the example. sorry my english. thanks
CODE GET DATA
foreach (glob("C://xampp/htdocs/LogMesinMutoh/*.job") as $file) {
$file_handle = fopen($file, "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $line;
}
fclose($file_handle);
}
DATA FILE
[JobSetting]
File=D:\Shared\print\2017\september\26\Printing Ira\Cat Pattern EKA SITOMPUL 55X55.tif
PrintSetup=VJ-1624W_MURIM_HP100_4PASS.tps
RaPInfoFile=VJ-1624
JobID=115518024
WorkType=3
SourceSizeX=549.980530
SourceSizeY=549.980530
DestSizeX=549.980530
DestSizeY=549.980530
I assume $str is a file content
$str = "File=D:\Shared\print\2017\september\26\Printing Ira\Cat
Pattern EKA SITOMPUL 55X55.tif
PrintSetup=VJ-1624W_MURIM_HP100_4PASS.tps RaPInfoFile=VJ-1624
JobID=115518024 WorkType=3 SourceSizeX=549.980530
SourceSizeY=549.980530 DestSizeX=549.980530 DestSizeY=549.980530";
$regex = '/JobID=(.*)/';
preg_match($regex, $str, $matches);
print_r($matches);
You will get your output at $matches[1]
I'm new in PHP. I'm trying to design a simple Glossary for Primary English Students.
I want to use a .txt as database, exploded by ":". I've got a txt like this:
Hola:Hello
Good Bye: Adios
Car:Coche
Banana:Plátano
Plane:Avión
By the moment I know how to print the whole text or one column, but can't print a single word:
<?php
$file = fopen("bank.txt", "r");
while(!feof($file)) {
echo fgets($file). "<br />";
}
fclose($file);
?>
How can I print only ONE SPECIFIC WORD?
e.g. What code is recquired to print only the second word of the third line?
Like mentioned in the comments... in the future you should inform yourself how to ask questions on SO.
Regarding your question, you should take a look at the explode-function and foreach-loops of PHP.
Example:
$data = 'Hola:Hello
Good Bye: Adios
Car:Coche
Banana:Plátano
Plane:Avión';
$arrData = array();
$lines = explode( "\n", $data );
foreach ($lines as $line) {
$words = explode( ":", $line );
$arrData[] = array( $words[0], $words[1] );
}
echo( $arrData[2][1] );
// Prints: "Coche"
This should work for you:
Just get your file into an array with file(). Then go through each line with array_map() and explode() the line by a : colon.
<?php
$lines = file("test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array_map(function($v){
return explode(":", $v);
}, $lines);
print_r($lines); //echo $lines[0][1] <- Will print the first line, second word
?>
Or what you could also do, is change your file into a .ini file format, e.g.
Hola="Hello"
Good Bye="Adios"
Car="Coche"
Banana="Plátano"
Plane="Avión"
And then you can simply use parse_ini_file(), e.g.
<?php
$file = parse_ini_file("test.ini");
//^^^^ See also here the extension
print_r($file);
?>
I have a file that looks like this:
1028806~HDR~20110815~15-AUG-2011~C~23:10~~~~~~~
1028806~DTL~C3914A~HWP-C3914A~1000949~A~LASERJET MAINT KIT 8100/N/DN~HEWLETT PACKARD~2659~12~0~0~475.75~658.75~0~3~Y~2~~2~475.75~5~~~009088336~~3179~10.60~N~8.25~8.50~20.50~~088698601976~44103109~6A~20030627~NNY~~A~S~~~~~~N~~~~~~20.50~8.50~8.25~~~~~~~~~~~~~~~~
1028806~DTL~70023301~OKI-70023301~1002121~A~OKILAN 6020E+ 10/100BASE-TX ETHERNET EXT~OKI PRINTING SOLUTIONS~2703~0~0~0~55.17~80.00~0~0~Y~0~~0~55.17~0~~~009117000~~2160~2.79~N~8.00~8.75~14.00~~000000180016~44101700~ACC-IMPACT~19950723~NNY~~A~S~~~~~~N~~~~~~14.00~8.75~8.00~~~~~~~~~~~~~~~~
1028806~DTL~PRO7T~APC-PRO7T~1003150~A~Professional-grade Protection for Computers and Electronics~AMERICAN POWER CONVERSION~20664~7~0~0~21.60~36.00~0~0~Y~0~~0~21.60~7~~~008112000~~4400~2.00~N~1.90~6.90~12.40~~731304000181~39121610~SURG~19950723~NNY~~A~S~~~~~~N~~~~~~12.40~6.90~1.90~~~~~~~~~~~~~~~~
1028806~DTL~PER7~APC-PER7~1003418~A~Surge suppressor ( external ) / 7 output connector(s)~AMERICAN POWER CONVERSION~20664~496~50~0~9.30~15.25~0~3~Y~86~~363~9.30~44~~~008118000~~4400~1.85~N~2.10~6.90~11.50~~731304000112~39121610~SURG~20011025~NNY~~A~S~~~~~~N~~~~~~11.50~6.90~2.10~~~~~~~~~~~~~~~~
1028806~DTL~PRO7~APC-PRO7~1003761~A~APC SurgeArrest Professional - Surge suppressor ( external ) - AC 120 V - 7 outp~AMERICAN POWER CONVERSION~20664~88~0~0~17.59~30.00~0~0~Y~12~~52~17.59~24~~~008112000~~4400~1.95~N~2.25~7.50~12.25~~731304000174~39121610~SURG~19950723~NNY~~A~S~~~~~~N~~~~~~12.25~7.50~2.25~~~~~~~~~~~~~~~~
I need to use a script to read certain parts of each line (the bold parts):
1028806~DTL~C3914A~HWP-C3914A~1000949~A~LASERJET MAINT KIT 8100/N/DN~HEWLETT PACKARD~2659~12~0~0~475.75~658.75~0~3~Y~2~~2~475.75~5~~~009088336~~3179~10.60~N~8.25~8.50~20.50~~088698601976~44103109~6A~20030627~NNY~~A~S~~~~~~N~~~~~~20.50~8.50~8.25~~~~~~~~~~~~~~~~
The file has over 300k items so going through manually is not an option, so how can I get a script to read only these parts when I don't know how long the part # and descriptions are? While ignoring all the other ~ characters.
Thanks
fgetcsv() can help here, a little more memory-conservative than loading the whole file up at once and explode()'ing all the lines into a giant array.
if (($handle = fopen("/path/to/file", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "~")) !== FALSE) {
echo $data[2] . " " . $data[6];
}
}
fclose($handle);
Looks like you can explode on tilde:
$fields = explode('~', $line);
$part_num = $fields[2];
$desc = $fields[6];
// read the file
$lines = file('file.txt');
// loop through each line
foreach($lines as $line){
// separate the parts by the ~ delimiter up to the second bold part
// ignoring the rest of ~
$parts = explode('~', $line, 7);
echo $parts[2]; // output first bold part
echo $parts[6]; // output second bold part
}
I have file with contents:
Tom TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student
Tom1 TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student
Tom2 TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student
I have many lines like this.
Each line is having a name followed by a string untill end of the line.
I want to have a php script that will read first words keeps in a temp variable.
similarly I want all the text upto the end of the line to be going to the second variable.
I have used these
$myFile = "profiles.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $theData;
but the issue with that is I am able to read the first 5 characters. I want to read first time only one word. second one is all string to be going to a variable.
Something like this?
$myFile = "profiles.txt";
$fh = fopen($myFile, 'r');
$anArray = array();
while (($line = fgets($fg)) !== false) {
list($name, $restOfLine) = explode(' ', $line, 2);
// Do something with $name and $restOfLine (e.g. put them in array)
$anArray[] = array('name' => $name, 'line' => $restofLine);
}
fclose($fh);
var_dump($anArray);
I have text file that looks like this:
1 1 1 1
1 2 3 5
4 4 5 5
I want to read this text file into array of lines and display it. Can anyone help me do this?
This should get you going: php function file
<?php
$cont = file_get_contents("data.txt");
$lines = explode("\n",$cont); // $lines is now an array containing each line
// do something with data here
?>
make sure you use the correct line endings however as Windows uses \r\n and UNIX uses \n.
you can use fopen(), fgets(). see here
eg
$f=fopen("file","r");
if($f){
while (!feof($f)) {
$line = fgets($f, 4096);
print $line;
}
fclose($f);
}
You'd want to do something like this:
<?
$filename = "somefile.txt";
$arr = file($filename);
foreach($arr as $line){
print $line . "<br />";
}
?>
This one is working for me.
$array = explode("\n", file_get_contents('file.txt'));