Jump to content

[SOLVED] text file searching question


irhy

Recommended Posts

Hi everyone,

 

I have a Unix text file like this:

 

 

#1 January 1.

[some text]

 

#2 January 2.

[some text]

 

etc...

 

#365 December 31.

[some text]

 

 

I want to write a simple webpage written in PHP that will check for the visitor's date and then display the corresponding entry in the file.  So if it's September 7th where you are, and you go to the webpage, it will show the entry for September 7th will be shown.  If it's February 29th, the entry for March 1st should be displayed.

 

I'm new to PHP and I've figured out the getting the date part, but I'm not sure how to do the searching the file part.  It's not a regular expression and it will be more than 1 line.  Can anyone point me in the right direction or have any ideas?  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/82884-solved-text-file-searching-question/
Share on other sites

little example get u going also a grate link for files ok m8.....

http://www.codewalkers.com/c/a/Programming-Basics/Working-with-text-files/

 

restricted words

 

<?php
$filename = "data.txt"; // File which holds all data
$inputString = "This is where you need to put some string which has bad words.";

$arrFp = file( $filename ); // Open the data file as an array
$numLines = count( $arrFp ); // Count the elements in the array

$arrWords = explode( ' ', $inputString ); // Split the input string into words as an array
$numWords = count( $arrWords ); // Count the words in the string

for($i=0; $i<$numWords; $i++) // Loop through the words in the string
{
    for($j=0; $j<$numLines; $j++) // Loop through the lines of the text file
    {
        if(strstr($arrWords[$i], trim( $arrFp[$j] ))) // Search whether the current words is restricted
            $arrWords[$i] = "*****"; // If it is replace the word with asterisks
    }
    
    $outputString .= $arrWords[$i].' ';
}
echo $outputString; // Echo the string replacing restricted words

?>

You can generate the search date using mktime() with a non leap year. That will take care of the Feb 29 problem

 

$searchDate = date ('F j', mktime (0,0,0, date('m'), date('d'), 2007));

 

As for the search bit

 

<?php

$searchDate = date ('F j', mktime (0,0,0, date('m'), date('d'), 2007));

$days = file('../test/datText.txt');              // read file into an array

foreach ($days as $k=>$line)
{
    if (strpos($line, $searchDate)) break;        // find the date
}
$text = '';                                       // read text until next date record
$line = $days[++$k] ;
while ($line{0} != '#' && isset($days[$k])) {
    $text .= $line;
    $line = $days[++$k];
} 

echo nl2br($text);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.