Viewed   363 times

Using php, I'm trying to create a script which will search within a text file and grab that entire line and echo it.

I have a text file (.txt) titled "numorder.txt" and within that text file, there are several lines of data, with new lines coming in every 5 minutes (using cron job). The data looks similar to:

2 aullah1
7 name
12 username

How would I go about creating a php script which will search for the data "aullah1" and then grab the entire line and echo it? (Once echoed, it should display "2 aullah1" (without quotations).

If I didn't explain anything clearly and/or you'd like me to explain in more detail, please comment.

 Answers

2

And a PHP example, multiple matching lines will be displayed:

<?php
$file = 'somefile.txt';
$searchfor = 'name';

// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches:n";
   echo implode("n", $matches[0]);
}
else{
   echo "No matches found";
}
Wednesday, October 19, 2022
1

Try the code below

$string = 'Dark Green = 11 = No = 20,
Light Blue = 12 = No = 20,
Lime Green = 13 = No = 20,
Sensei Gray = 14 = Yes = 0';


$string = explode(',',$string);
foreach($string as $row)
{
    preg_match('/^(D+)s=s(d+)s=s(D+)s=s(d+)/', trim($row), $matches);
    echo $matches[1];//Dark Green
    echo $matches[2];//11
    echo $matches[3];//No
    echo $matches[4];//20
}

In the loop use to check the word to search

Like that

if($matches[1] == 'Dark Green')
{
   echo $matches[1];
}

or

   if($matches[2] == 11)
    {
       echo $matches[2];
    }

(...) To get the text in the file try use

    $string = file_get_contents('file.txt');
Wednesday, October 19, 2022
 
ali_m1
 
5

In the create_zip() function, change

foreach($valid_files as $file) {
    $zip->addFile($file,$file);
}

to

foreach($valid_files as $file) {
    $zip->addFile($file,pathinfo($file,PATHINFO_BASENAME));
}

This will work as long as you don't have files with duplicate names but different directories in your array of files

Monday, December 19, 2022
 
4

I actually wrote a function for this a few days ago...

Here's the base function that scans each file...

foreach (glob("<directory>/*.txt") as $search) {
    $contents = file_get_contents($search);
    if (!strpos($contents, "text")) continue;
    $matches[] = $search;
}

Not the most advanced way of doing it, my function is much longer but it also uses all functions from my various other classes, this is basically what it does though.

Tuesday, August 2, 2022
 
micbobo
 
5

I know it is already answered :) To summarize the above:

# It is a good idea to store the filename into a variable.
# The variable can later become a function argument when the
# code is converted to a function body.
filename = 'data.txt'

# Using the newer with construct to close the file automatically.
with open(filename) as f:
    data = f.readlines()

# Or using the older approach and closing the filea explicitly.
# Here the data is re-read again, do not use both ;)
f = open(filename)
data = f.readlines()
f.close()


# The data is of the list type.  The Python list type is actually
# a dynamic array. The lines contain also the n; hence the .rstrip()
for n, line in enumerate(data, 1):
    print '{:2}.'.format(n), line.rstrip()

print '-----------------'

# You can later iterate through the list for other purpose, for
# example to read them via the csv.reader.
import csv

reader = csv.reader(data)
for row in reader:
    print row

It prints on my console:

 1. 12,12,12
 2. 12,31,12
 3. 1,5,3
-----------------
['12', '12', '12']
['12', '31', '12']
['1', '5', '3']
Monday, October 10, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :