Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6333547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:28:57+00:00 2026-05-24T18:28:57+00:00

I have been struggling to create a Simple ( really simple ) chat system

  • 0

I have been struggling to create a Simple ( really simple ) chat system for my website as my knowledge on Javascripting/AJAX are Limited after gather resources and help from many kind people I was able to create my simple chat system but left with one problem.

The messages are posted to a file called “msg.html” in this format :
<p><span id="name">$name</span><span id="Msg">$message</span></p>
And then using PHP and AJAX I will retrieve the messages instantly from the file using the
file(); function and a foreach(){} loop withing PHP here is the code :

    <?php 
$file = 'msg.html';
$data = file($file);
$max_lines = 20;
if(count($data) > $max_lines){
    // here i want the data to be deleted from oldest until i only have 20 messages left.
}
foreach($data as $line_num => $line){
echo $line_num . " . " . $line; 
}
?>

My Question is how can i delete the oldest messages so that i am only left with the latest 20 Messages ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:28:58+00:00Added an answer on May 24, 2026 at 6:28 pm

    How does something like this seem to you:

    $file = 'msg.html';
    $data = file($file);
    $max_lines = 20;
    foreach($data as $line_num => $line)
    {
        if ($line_num < $max_lines)
        {
            echo $line_num . " . " . $line; 
        }
        else
        {
            unset($data[$line_num]);
        }
    }
    file_put_contents('msg.html', $data);
    ?>
    

    http://www.php.net/manual/en/function.file-put-contents.php for more info 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a very simple question, but I have been struggling with
I have been struggling for too long a time now with a rather simple
I have been struggling to create a django form which users can fill and
So I have been struggling for days now, trying to simply create a new
I have been struggling to create a Dropdown list which will display Country names
I have been struggling with trying to create/save multiple instances at once in Grails,
Have been struggling with Javascript closure for a while trying to wrap brain around
I have been struggling with the best way to make sure that the certain
I have been struggling to think of some decent uses for things like vectors
I have been struggling with this question for awhile now, and I haven't reached

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.