PHP om in txt-bestand te zoeken en de hele regel te herhalen

Met behulp van php probeer ik een script te maken dat in een tekstbestand zal zoeken en die hele regel zal pakken en herhalen.

Ik heb een tekstbestand (.txt) met de titel “numorder.txt” en binnen dat tekstbestand zijn er verschillende regels met gegevens, met elke 5 minuten nieuwe regels (met behulp van cron job). De gegevens lijken op:

2 aullah1
7 name
12 username

Hoe zou ik een php-script maken dat zoekt naar de gegevens “aullah1” en dan de hele regel pakken en herhalen? (Eenmaal herhaald, zou het “2 aullah1” moeten weergeven (zonder aanhalingstekens).

Als ik iets niet duidelijk heb uitgelegd en/of je wilt dat ik het in meer detail uitleg, reageer dan alsjeblieft.


Antwoord 1, autoriteit 100%

En een PHP-voorbeeld, er worden meerdere overeenkomende regels weergegeven:

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

Antwoord 2, autoriteit 69%

Doe het zo. Met deze aanpak kunt u een bestand van elke groottedoorzoeken (groot formaat crasht het script niet) en retourneert ALLE regels die overeenkomenmet de gewenste tekenreeks.

<?php
$searchthis = "mystring";
$matches = array();
$handle = @fopen("path/to/inputfile.txt", "r");
if ($handle)
{
    while (!feof($handle))
    {
        $buffer = fgets($handle);
        if(strpos($buffer, $searchthis) !== FALSE)
            $matches[] = $buffer;
    }
    fclose($handle);
}
//show results:
print_r($matches);
?>

Let op de manier waarop strposwordt gebruikt met !==operator.


Antwoord 3, autoriteit 26%

Met behulp van file()en strpos():

<?php
// What to look for
$search = 'foo';
// Read from file
$lines = file('file.txt');
foreach($lines as $line)
{
  // Check if the line contains the string we're looking for, and print if it does
  if(strpos($line, $search) !== false)
    echo $line;
}

Bij testen op dit bestand:

foozah
barzah
abcza

Het geeft het volgende weer:

foozah


Bijwerken:
Gebruik iets als dit om tekst weer te geven als de tekst niet wordt gevonden:

<?php
$search = 'foo';
$lines = file('file.txt');
// Store true when the text is found
$found = false;
foreach($lines as $line)
{
  if(strpos($line, $search) !== false)
  {
    $found = true;
    echo $line;
  }
}
// If the text was not found, show a message
if(!$found)
{
  echo 'No match found';
}

Hier gebruik ik de variabele $foundom te zien of er een overeenkomst is gevonden.


Antwoord 4, autoriteit 9%

$searchfor = $_GET['keyword'];
$file = 'users.txt';
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if (preg_match_all($pattern, $contents, $matches)) {
    echo "Found matches:<br />";
    echo implode("<br />", $matches[0]);
} else {
    echo "No matches found";
    fclose ($file); 
}

Antwoord 5, autoriteit 5%

het lijkt erop dat u beter kunt systematiseren naar system("grep \"$QUERY\"")aangezien dat script hoe dan ook niet bijzonder goed presteert. Anders http://php.net/manual/en/function.file.phplaat zien hoe je over lijnen heen kunt lopen en je kunt http://php.net gebruiken /manual/en/function.strstr.phpvoor het vinden van overeenkomsten.


Antwoord 6, autoriteit 4%

enkele reis…

$needle = "blah";
$content = file_get_contents('file.txt');
preg_match('~^(.*'.$needle.'.*)$~',$content,$line);
echo $line[1];

hoewel het waarschijnlijk beter zou zijn om het regel voor regel te lezen met fopen() en fread() en strpos() te gebruiken

Other episodes