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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:23:06+00:00 2026-06-14T18:23:06+00:00

Hi this is my problem i want to read from a file till i

  • 0

Hi this is my problem i want to read from a file till i reach a specific character and then write a string in a new line before that specific character with php i know how to read by fopen i also know how to read line by line i dont know the last part(inserting my string in the line before that)
Look at This example please:
MYfile Contains:

Hello 
How are You 
blab la 
...
#$?!
other parts of my file...

so know i want it to when it reached $?! put my string in the line before that assume that my string is I did it!

Now MYfile Contains:

Hello 
How are You 
blab la 
...
#I did it!
#$?!
other parts of my file...

How should i do this?!?
What i’ve done so far:

$handle = @fopen("Test2.txt", "r");
if ($handle) 
{
while (($buffer = fgets($handle, 4096)) !== false) 
{
    if($buffer == "#?") echo $buffer;
}
if (!feof($handle)) {
    echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
  • 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-14T18:23:09+00:00Added an answer on June 14, 2026 at 6:23 pm

    You simply need to search for the $?! when reading the text.

    As you are reading line by line, check it in each line.

    Personally, I would read the whole file at once (assuming it is not too big) and replace the string with the vlaue required.

    $needle = '$?!'; // or whatever string you want to search for
    $valueToInsert = "I did it!"; // Add \r\n if you need a new line
    
    $filecontents = file_get_contents("Test2.txt"); // Read the whole file into string
    $output = str_replace($needle, $valueToInsert . $needle, $filecontents);
    
    echo $output; // show the result
    

    Not tested the above code – might require tweaking.

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

Sidebar

Related Questions

I have this problem: I want to generate a new source code file from
This program want to read from a file. the content in the file is
I have code which uses the StreamReader to read HTML from a file, then
How do I read data from a file if my file is like this
I have a file containing java objects , wrote with this code: from(somewhere).process(new Processor()
In my android project, i want to read a data structure from file. A
I read from file store the data into a list; I want to add
I want to read 8 byte from a file. StreamReader^ sr = gcnew StreamReader(
I have a list of object properties read from xml file and want to
i am using xml parser to read data from xml file. In this file

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.