I have an archive txt in this format.
$line[0] = 50009828720001007029552330034 20181009MG551 0119195102P000000002624400000000000000000000000000000000000000000000000262440000N
In this line I can get many information of a determinated document.
Example: When I use the functions below I can get the number of document, date of document, and another information.
$numDoc[0] = (string)substr($line[0], 46, 5); // THE NUMBER OF DOCUMENT IS 011919
$dateDoc[0] = (string)substr($line[0], 30, 8); // DATE OF DOCUMENT IS 20181009
How I can structure my archive to easy print the information ordered like this.
NUMBER OF DOCUMENT - DATE DOCUMENT
What the easy way to ordered this information, is a small application does not allow user make anything, just upload this archive .txt.
My question is about structure the informations for easy acess, like bellow:
doc1{
number = xxx
date = xxx
generic = xxx
}
I assume you need a way to make it more dynamic than manually typing [0] then [1] and so on?
You can loop the txtfile with foreach and assign new array items with [].
$file = fopen("path of txt");
foreach($file as $line){
$numDoc[] = substr($line, 46, 5);
$dateDoc[] = substr($line, 30, 8);
}
The code above will loop through the document and create two arrays with the dates and the document number.
I would probably make it an multidimensional array instead to keep all in one place.
foreach($file as $line){
$array["numdoc"][] = substr($line, 46, 5);
$array["dateDoc"][] = substr($line, 30, 8);
}
Related
I have current module where I need to list all zip files from S3 AWS to the html tables. now I want to sub-string the 2 digit and the last digit of the number. however when i try to var dump the result still same number the substr not working can you help me guys to find out how to solved this?.
Example.
Number: 1150
Result must be: 11 and 50
I will show you guys my sample code that I already created.
$storage = Storage::disk('s3');
$client = $storage->getAdapter()->getClient();
$command = $client->getCommand('ListObjects');
$command['Bucket'] = $storage->getAdapter()->getBucket();
$command['Prefix'] = '11-10-2019';
$result = $client->execute($command);
foreach ($result['Contents'] as $file) {
$base_name = basename($file['Key']);
$trim_1 = str_replace('exp', '', $base_name);
$trim_2 = substr($trim_1, 1, -4);
var_dump($trim_2);
}
My Output is this:
$number = 1150;
$trim1 = substr($number, 0, 2);
$trim2 = substr($number,-2);
echo $trim1;
echo $trim2;
Based on what you ask you can achieve this by doing something like that.
The output is 11 & 50
Since you are using laravel you can use laravel helper for that which is pretty much the same and go for something like:
Str::substr($number, 2);
Str::substr($number, -2);
Don't forget to include the helper at the top of your file:
use Illuminate\Support\Str;
I have a text file like this:
1
2
3
4
5
6
7
8
9
10
And I want to remove specific lines which numbers are in an array like this:
$myfile='txt.txt';
$remove=array(1,3,6,7,10);
//wanna remove these lines
So I tried this code but It didn't work and It just doubles the text and ruins everything:
<?php
$myfile='txt.txt';
$remove=array(1,3,5,7,10);
$lines=file($myfile);
$countline=sizeof($lines);
$data=file_get_contents($myfile);
for ($i=0; $i < $countline+1; $i++) {
if (in_array($i, $remove)) {
$editeddata=str_replace($lines[$i], "", $data);
$removeline = file_put_contents($myfile, $editeddata.PHP_EOL , FILE_APPEND | LOCK_EX);
}
}
?>
I couldn't use ((for)) properly and I think it will just ruin the text because it deletes lines one after another have been deleted and it changes the order so I should have a code to remove them all at once.
And please don't give a code to just replace numbers because the main text file is not only numbers and contains word,etc...
Thanks A lot!
You're reading the file twice (with file and file_get_contents), which I think is confusing the later code. You have everything you need with the first call - an array of all the lines in the file. You're also using str_replace to remove the content, which seems a bit dangerous if any of the content is repeated.
I'd refactor this to simply filter the array of lines based on their line-number, then write it back to the file in a single operation:
$myfile = 'txt.txt';
$remove = [1, 3, 5, 7, 10];
// Read file into memory
$lines = file($myfile);
// Filter lines based on line number (+1 because the array is zero-indexed)
$lines = array_filter($lines, function($lineNumber) use ($remove) {
return !in_array($lineNumber + 1, $remove);
}, ARRAY_FILTER_USE_KEY);
// Re-assemble the output (the lines already have a line-break at the end)
$output = implode('', $lines);
// Write back to file
file_put_contents($myfile, $output);
If the file fits in memory then you can do the simple:
$myfile='txt.txt';
$remove=array(1,3,6,7,10);
file_put_contents($myfile, implode(PHP_EOL,array_diff($file($myfile,FILE_IGNORE_NEW_LINES), $remove)));
Note: Because it's a bit ambiguous whether $remove has the content or the lines you want to remove, the above code removes the content . If you want to remove lines change array_diff($file($myfile,FILE_IGNORE_NEW_LINES), $remove) to array_diff_keys($file($myfile,FILE_IGNORE_NEW_LINES), array_flip($remove))
If your file is large then you need to resort to some sort of streaming. I suggest against reading and writing to the same file and doing something like:
$myfile='txt.txt';
$remove=array(1,3,6,7,10);
$h = fopen($myfile,"r");
$tmp = fopen($myfile.".tmp", "w");
while (($line = fgets($h)) !== false) {
if (!in_array(rtrim($line, PHP_EOL), $remove)) {
fwrite($tmp, $line);
}
}
fclose($h);
fclose($tmp);
unlink($myfile);
rename($myfile.".tmp", $myfile);
I got a bit of a complex problem. At work we have to count our inventory every month. This is done with a scanner. At each location there can be up to 100 different items. Every item, even the same kind have to be scanned. When each location has been scanned, we print out the list of scanned items. The problem is that each scan has its own line in the txt file (it done not add/subtract multiple counts of the same item)
As the vendor of our system is notoriously slow implementing new functions I thought about a php script that does the following:
1: read every line from the txt file
2: add/substract the count of the same item
3: print out a list with the item number and count.
The txt file is as following:
01234+000001N
Where the first 5 digits is the item number. As it is possible to add and substract the next symbol is + or - then the next 5 digits is the count and the N is the "eol"
So somehow I have to put it all in some sort of array and the sort it by item number. And the add/substract and then finally print out the final list
Assuming you've loaded the file into a string, line by line, and is split by a new line, you can do the following; (read code comments)
$strTxtFile = <<<TXT
01234+000001N
01234+000001N
09876+000002N
01234+000001N
01234+000001N
09876+000002N
01234-000001N
09876+000002N
TXT;
/**
* 01234 should have 3 stock
* 09876 should have 6 stock
*/
$arrProducts = array();
$arrFileLines = explode(PHP_EOL, $strTxtFile);
foreach($arrFileLines as $strFileLine) {
//Split the lines by the action (+/-)
$arrStockAction = preg_split("/(\+|\-)/", $strFileLine, NULL, PREG_SPLIT_DELIM_CAPTURE);
$strProductCode = $arrStockAction[0]; //The first part is the product code
$strAction = $arrStockAction[1]; //The action (+/-) to the stock
$intStockAmount = (int) $arrStockAction[2]; //Cast it to an int to get the number
//Check if the product exists in our array, if not, create it with 0 stock
if( array_key_exists($strProductCode, $arrProducts) === FALSE ) {
$arrProducts[$strProductCode] = 0;
}
if($strAction === "+") {
//Add stock
$arrProducts[$strProductCode] += $intStockAmount;
} else {
//Minus stock
$arrProducts[$strProductCode] -= $intStockAmount;
}
}
print_r($arrProducts);
https://repl.it/ECrW
Similar to the other answer, maybe a little simpler:
foreach(file('/path/to/file.txt') as $line) {
$item = substr($line, 0, 5);
$sign = substr($line, 5, 1);
$qty = substr($line, 6, 6);
if(!isset($result[$item])) {
$result[$item] = $qty;
} else {
$result[$item] += $sign.$qty;
}
}
Or replace the substr() lines with:
preg_match('/(\d{5})(.)(\d{6})/', $line, $matches);
And use $matches[1], $matches[2] and $matches[3].
I just found out I had misread the txt file. The lines is as follow:
01234000001 N
And
01234000001-N
The blank space between the last number and the N represent addition and - substract
I have searched for hours and cannot find the answer.
I have 2 arrays that will always be the same length
array_a(12, 13, 14);
array_b(15, 18, 20);
How would I print to 3 different variables (or arrays) like so...
$array_c(12, 15);
$array_d(13, 18);
$array_e(14, 20);
So they line up.
Thanks
Okay I figured it out. I guess I should explain what I was trying to
do better so that the code is easier to understand. I have an
application in which a user can enter room sizes for the square
footage of a room. Because all rooms are not always square, I wanted
to be able to allow the user who had a "L" shaped room to divide the
room up and enter multiple measurements for each room. So they could
add another row to a table to add the length and width for the bottom
of the "L". (I know its hard to explain) Anyway, the user could submit
2 squares for a room that is odd shaped. Since my database only has 1
row per room, I figured it would be better to add multiple measurements inside of the same field separated by a space which I could explode later if I need to. Anyway, this is what I came up with
$width = $_POST['width']; //an array
$length = $_POST['length']; //an array
$dimensions_array = array();
foreach ($width as $key => $value) {
$individual_length = $length[$key];
array_push($dimensions_array, $value.'x'.$individual_length);
}
$dimensions = implode(' ', $dimensions_array);
This returns an array called $dimensions that is (12x15 13x18 14x20)
$width = $_POST['width']; //an array
$length = $_POST['length']; //an array
$dimensions_array = array();
foreach ($width as $key => $value) {
$individual_length = $length[$key];
array_push($dimensions_array, $value.'x'.$individual_length);
}
$dimensions = implode(' ', $dimensions_array);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse a text file containing image data
Here is my code for getting image data from a text file:
while (!feof($fh)) {
$line = fgets($fh);
$lines[] = $line;
$match1 ="/^[0-9]{1,3},[0-9]{1,3}/";
$match2 = "/[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},(?:,?[0-9]{1,3})*(?:\.[0-9]{1,10})?\b/";
$parts = preg_match($match1, $line, $regs);
$parts2 = preg_match($match2, $line, $regs2);
foreach($regs as $key => $lame) {
$lamer[] = $lame;
}
foreach($regs2 as $key => $lame2) {
$lamer2[] = $lame2;
}
}
The first preg_match gets the coords, and second gets the rgba() data.
I'm trying to put this into a javascript array but I am getting this error:
SyntaxError: too many constructor arguments
I assume it's too much data for the javascript array.
Now I am wondering how or if I can skip data in the array, namely the the coords
that have a rgba with 0 alpha, which would mean that I would have to skip both.
I'm also wondering if I should try to combine the two matches into one to see if that would make it easier, but I'm not sure how to do that.
Here's the data I am working with that is a 300x180 image:
41,6: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0)
42,6: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0)
90,35: ( 77, 80, 12, 98) #4D500C62 srgba(77,80,12,0.384314)
91,35: ( 95, 99, 13, 78) #5F630D4E srgba(95,99,13,0.305882)
92,35: ( 96, 99, 31, 90) #60631F5A srgba(96,99,31,0.352941)
93,35: (106,110, 14, 68) #6A6E0E44 srgba(106,110,14,0.266667)
94,35: ( 95, 99, 13, 78) #5F630D4E srgba(95,99,13,0.305882)
Use JavaScript RegExp pattern
^(\\d+),(\\d+)[^#]+#.{6}(?!00)[^(]+\\((\\d+),(\\d+),(\\d+),(\\d*(?:\\.\\d*)?)
Check this demo.