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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:42:48+00:00 2026-05-30T04:42:48+00:00

I have a PHP script that reads the contents of a file and stores

  • 0

I have a PHP script that reads the contents of a file and stores it into a variable. I want to split the text stored by the new line entries and then search the array[0] value for a specific value.

That is my idea. Here is my situation.

I have a directory file with specific entries.

Entry1_two_term3_stuff
Entry2_two_term3_stuff
Entry3_two_term3_stuff

This will be stored into a variable. How do I search this for Entry2 and then get the whole line to work with?

  • 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-30T04:42:50+00:00Added an answer on May 30, 2026 at 4:42 am

    If you can, use the file() function to load the file into array, already split by newlines:

    $lines = file('filename.txt');
    

    If not, you can use

    $lines = explode("\n", $contents);
    

    You might want to use \r\n if you’re on Windows and file contains CR characters.

    To search the array, use foreach:

    foreach ($lines as $ix => $line)
        if (strpos ($line, 'string') !== false)
            $lines[$ix] = ...new version of line...
    

    In the end, write contents back to some file:

    $fp = fopen('file.txt', 'w+');
    foreach ($lines as $line)
        fputs($fp, $line);
    fclose($fp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that is reading a file in (file_get_contents). I want
I have an autofill search that reads the contents of a text file (this
I have a PHP script that reads a file and outputs it to the
I have a PHP script that reads in data from an XML file, returns
I have a php script that reads an XML file and modifies it with
I have a PHP script that reads from a pipe in blocking mode. while($input
I have a written a script in PHP that reads from data from a
I have a PHP script that executes a .bat file using system(cmd /c C:\dir\file.bat);
I have an mobile app that reads a JSON file that is stored on
I have a jQuery tab script that gets content from a PHP file defined

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.