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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:21:44+00:00 2026-05-13T16:21:44+00:00

I have a text file and I want to remove some lines that contain

  • 0

I have a text file and I want to remove some lines that contain specific words

 <?php
// set source file name and path
$source = "problem.txt";

// read raw text as array
$raw = file($source) or die("Cannot read file");

now there’s array from which I want to remove some lines and want to use them so on.

  • 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-13T16:21:45+00:00Added an answer on May 13, 2026 at 4:21 pm

    As you have each line of your file in a row of an array, the array_filter function might interest you (quoting) :

    array array_filter  ( array $input  [, callback $callback  ] )
    

    Iterates over each value in the input
    array passing them to the callback
    function.
    If the callback
    function returns true, the current
    value from input is returned into the
    result array. Array keys are
    preserved.

    And you can use strpos or stripos to determine if a string is contained in another one.

    For instance, let’s suppose we have this array :

    $arr = array(
      'this is a test',
      'glop test',
      'i like php',
      'a badword, glop is', 
    );
    

    We could define a callback function that would filter out lines containing “glop” :

    function keep_no_glop($line) {
      if (strpos($line, 'glop') !== false) {
        return false;
      }
      return true;
    }
    

    And use that function with array_filter :

    $arr_filtered = array_filter($arr, 'keep_no_glop');
    var_dump($arr_filtered);
    

    And we’d get this kind of output :

    array
      0 => string 'this is a test' (length=14)
      2 => string 'i like php' (length=10)
    

    i.e. we have removed all the lines containing the “badword” “glop”.

    Of course, now that you have the basic idea, nothing prevents you from using a more complex callback function 😉


    Edit after comments : here’s a full portion of code that should work :

    First of all, you have your list of lines :

    $arr = array(
      'this is a test',
      'glop test',
      'i like php',
      'a badword, glop is', 
    );
    

    Then, you load the list of bad words from a file :

    And you trim each line, and remove empty lines, to make sure you only end up with “words” in the $bad_words array, and not blank stuff that would cause troubles.

    $bad_words = array_filter(array_map('trim', file('your_file_with_bad_words.txt')));
    var_dump($bad_words);
    

    The $bad_words array contains, from my test file :

    array
      0 => string 'glop' (length=4)
      1 => string 'test' (length=4)
    

    Then, the callback function, that loops over that array of bad words:

    Note : using a global variable is not that nice 🙁 But the callback function called by array_filter doesn’t get any other parameter, and I didn’t want to load the file each time the callback function is called.

    function keep_no_glop($line) {
      global $bad_words;
      foreach ($bad_words as $bad_word) {
          if (strpos($line, $bad_word) !== false) {
            return false;
          }
      }
      return true;
    }
    

    And, as before, you can use array_filter to filter the lines :

    $arr_filtered = array_filter($arr, 'keep_no_glop');
    var_dump($arr_filtered);
    

    Which, this time, gives you :

    array
      2 => string 'i like php' (length=10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that I want to edit by rewriting it to
I have a text file that I want to edit using Java. It has
I have a text file that I want to send over the network, this
I have a text file that's appended to over time and periodically I want
I have a text file. I want read that file. But In that if
I have a text file which contains a long list of words. Some of
I have a file like this: my line - some words & text oh
I have a huge text file ( 375K lines ). All I want is
I have a programming text editor (written in PHP) and want to remove the
have downloaded Orca to edit an MSI file. I want to remove some banner

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.