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 4099422
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:25:54+00:00 2026-05-20T20:25:54+00:00

I am working on a small project and I need some help. I have

  • 0

I am working on a small project and I need some help. I have a CSV file with 150,000 rows (each row has 10 cols of data). I am using the fscvread to read the file and during the loop I want to match one of the columns (call it stringx) of each row against an array of 10,000 words. If any of the 10,000 words exist in stringx, it is removed using preg_replace.

Now all of this is fine. I have everything working fine, but the problem is, its too slow.

I have tried 2 methods to match the array.
1) I convert stringx to an array using explode(” “, $stringx) and then use the array_diff($array_stringx, $array_10000);
2) use foreach on $array_10000 and preg_replace on $stringx

Method 1 takes about 60 secs to go through 200 rows of data and method 2 can loop 500 rows in 60 secs.

Is there a better way to do this?

Once again, I am looking for an efficient way to (basically) array_diff an array of 10,000 words against 150,000 strings one at a time.

Help is much appreciated.

  • 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-20T20:25:55+00:00Added an answer on May 20, 2026 at 8:25 pm

    The following is just an alternative. It may or may not fulfil your requirements.

    It performs 84 ops/second with 10k words dictionary and 15k string on my laptop.

    Downside is that it does not remove the spaces around the words.

    $wordlist is just rows with one word each, could be a file.

    $dict = array_flip(preg_split('/\n/',$wordlist));
    
    function filter($str,$dict) {
      $words = preg_split('/\s/',$str);
      sort($words);
      $words = array_unique($words);
    
      foreach ($words as $word) {
        if (key_exists($word,$dict)) {
            $removeWords[] = '/\b' . $word . '\b/';
        }
      }
      return preg_replace($removeWords, '', $str);
    }
    

    Another example that performs a bit faster (107ops/s with 15kb string and 10k words dictionary)

    function filter2($str,$dict) {
      $words = preg_split('/\b/',$str);
      foreach ($words as $k => $word) {
        if (key_exists($word,$dict)) {
            unset($words[$k]);
        }
      }
      return implode('', $words);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that appends some rows to a table. One of the
I'm working on a project in C# w/ XNA, and I want to reorganize

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.