Reading specific value from a text file extensi .job - php

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]

Related

How can I read contents of a text file using PHP & process elements?

I'm trying to read in a name from a text file in PHP and remove spaces in the names, which I do using preg_replace in a function.
function fixName($input) {
$input = preg_replace('/[^ \w-]/', ' ', $input);
return $input;
}
echo fixName('BOB VAN'), "\n";
However, I have a text file that has multiple names similar to the one above each on a new line, and I'm trying to figure out how to store those names that are read in from the file and process and output them. Would I do something such as reading the file and storing them into an array?
The text file looks like this
ID FIRST LAST
348 BOB VAN
349 ALBERT JOHN
If you want to read file string by string and store it in an array then you can try something like:
$output = array();
$handle = fopen("file.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle);
$buffer = preg_replace('/[^ \w-]/', ' ', $buffer);
list($id, $name) = explode(' ', $buffer, 2);
$output[$id] = $name; //you can index elements by ID or store ID and name as nested array
}
fclose($handle);
var_dump($output);

Read .txt table from PHP

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);
?>

Make Php string without break lines

i need to generate random sentences from dictionary. In dictionary is every word at one line, firstly i load this dictionary to array and after it i have a for cycle and randomly pickup some data, but if i wrote it, so it is at one line in browser, but in source code is every word at another line. Then I need to create a set of XML files from search engine and this new lines are indexed as /n/r and in XML source code it has got a symbol 
 So my question is how i can make a sentence which will be at one line in source code too. Thanks.
Here is piece of my code i don´t have here randomly loading data, i only made it for illustration in for cycle.
$file = fopen("test.txt", "r");
$data = array();
while (($buffer = fgets($file)) !== false) {
$data[] = $buffer;
}
$sentence = '';
for ($i=0;$i<10;$i++){
$sentence = $sentence . $data[$i];
}
Use trim function to filter new line characters.
In your code use:
$data[] = trim($buffer);

PHP: Reading word by word?

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";
}

preg_match exact word and print out the entire line the result was found in

i'm having a hard time understanding how to do this.
I want to search for a word, and then I want the search to return the word along with all the contents on the line the word was found in.
The word needs to be case sensitive so searching for TOM will not return Tom along with the results.
this is what I have tried thus far.
$contents = file_get_contents($file);
$pattern = preg_quote($word, '/');
$numInt = 0;
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches))
{
$numInt = count($matches[0]);
}
When I run this through my function it returns the results I want, but I noticed that it isn't consistant across different keyword.
In my text file for testing I have INT (x18) and my function returns 18 as the count. But if I use the keyword MORNING (x29) the function returns 31 which is technically correct because there are 2 instances where morning is used.
Well you could avoid using regex all together for this task.
Here's some code I whipped up:
<?php
$contents = file_get_contents($file);
#standardise those line endings
$contents = str_replace(array("\r","\r\n","\n\r","\n"),"\n",$contents);
$lines = explode("\n", $contents);
#find your result
$result = $line_num = array();
foreach($lines as $line_num => $l)
if(strpos($l, $word)) {
$result[] = $l;
$line_nums[] = $line_num;
}
echo "<pre>";
print_r($result);
echo "<br>Count:".count($result);

Categories