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

  • Home
  • SEARCH
  • 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 9301303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:59:03+00:00 2026-06-18T22:59:03+00:00

I would like to ask you about some known PHP libraries which may help

  • 0

I would like to ask you about some known PHP libraries which may help me to parse *.txt files for sentences.
I have to parse too large text files, so I decided to make a stream parser (sentence by sentence).

I thought that it would be pretty to iterate file by sentences, something like:

foreach (new SentenceIterator("./data/huge.txt") as $sentence)
{
    // do something...
}

Main idea is that file should be load to the memory completely.

What I have tried:

$f = fopen("./data/huge.txt", "r");
$dataBytes = 64;
$buffer = '';

while (!feof($f))
{
    $data = fread($f, $dataBytes);
    $dotPosition = strpos($data, '.');

    if (false !== $dotPosition)
    {
        $sentence = $buffer . substr($data, 0, $dotPosition);
        // correct cursor position
        fseek($f, -1 * $dotPosition, SEEK_CUR);
        // clear buffer
        $buffer = '';
        continue;
    }

    $buffer .= $data;
}

But in this case I get corrupted (lopped) sentences.

Could someone suggest me some existing libraries or maybe how to fix my code?

Thx in advance.

  • 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-06-18T22:59:04+00:00Added an answer on June 18, 2026 at 10:59 pm

    Sorry for inconvenience,

    After some digging I have found solution which is… Spl lib..

    Iterator called SplFileObject which implements Iterator, RecursiveIterator and SeekableIterator. And it allows read file line by line.

    Updates and worked code is:

    $file = new SplFileObject('./data/test.txt');
    $file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
    $buffer = '';
    
    foreach ($file as $lineNumber => $line)
    {
        $dotPos = strpos($line, '.');
        if (false !== $dotPos)
        {
            $sentence = $buffer . substr($line, 0, $dotPos);
            echo $sentence . "\n";
            $buffer = substr($line, $dotPos);
            continue;
        }
        $buffer .= $line;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask you about some advices about this code. It works,
I would like to ask to you to share some ideas about how do
I would like to ask about some advices, how to use Symfony2 SonataAdminBundle. I
There are some discussions about the same question but I would like to ask
I would like to ask about the available (free or not) Static and Dynamic
I would like to ask you about regular expressions preg_match have outlined below. I
i would like to ask a question about @UsesJAXBContext annotation in jax-ws. I try
I have been reading about unsafePerformIO lately, and I would like to ask you
I would like to ask such question, I have XML xsd`s, which generate beans
I would like to ask a question about a new and large scale web

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.