Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.