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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:16:50+00:00 2026-06-09T17:16:50+00:00

This is my first post on the internet for some assistance with coding so

  • 0

This is my first post on the internet for some assistance with coding so please bear with me!
I have been finding open code on the internet for a few years and modding it to do what I want but I seem to have come up against a wall with this one that I am sure is very simple. If you would please be able to help me it would be very much appreciated.

I have the following page:

    <?php 
    $text = $_REQUEST['message'];
    $f = file_get_contents("all.txt"); 
    $f = explode(", ", $f); 

    function modFile($pos, $tothis, $inthis)
    { 
        foreach($inthis as $pos => $a){ 
        } 
        $newarr = implode("\r\n", $inthis); 
        $fh = fopen("example.txt", "w"); 
        fwrite($fh, $newarr); 
        fclose($fh); 
    } 

    modFile(4, '', $f); 

I have a file (all.txt) with the following:

    11111111111, 22222222222, 33333333333, 44444444444

That I wish to display like this:

    11111111111 
    22222222222
    33333333333
    44444444444

and to add a space then some text after each number where the text is the same on each line:

    11111111111 text here
    22222222222 text here
    33333333333 text here
    44444444444 text here

I have an html form that passes the custom text to be appended to each line.
I need to keep the file all.txt intact then save the newly formatted file with a different name.

I have tried putting variables into the implode where I currently have the “\r\n” but this does not work.

Any help very much appreciated.

Thanks 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-09T17:16:52+00:00Added an answer on June 9, 2026 at 5:16 pm

    A few notes about your code: You are passing $pos to the function but it will get overwritten in the foreach. Also the foreach is empty, so what’s it good for? And I don’t see you use $text anywhere either.

    To achieve your desired output, try this instead:

    file_put_contents(
        '/path/to/new.txt',
        preg_replace(
            '/[^\d+]+/',
            ' some text' . PHP_EOL,
            file_get_contents('all.txt')
        )    
    );
    

    The pattern [^\d+]+ will match any string that is not a consecutive number and replace it with “some text ” and a new line.

    A somewhat more complicated version achieving the same would be:

    file_put_contents(
        '/path/to/new.txt', 
        implode(PHP_EOL, array_map(
            function ($number) {
                $message = filter_var(
                    $_POST['message'], FILTER_SANITIZE_SPECIAL_CHARS
                );
                return sprintf('%s %s', trim($number), $message);
            },
            array_filter(str_getcsv(file_get_contents('/path/to/all.txt')))
        )
    ));
    

    This will (from the inside out):

    • Load the content of all.txt and parse it as CSV string into an array. Each array element corresponds to a number.
    • Each of these numbers is appended with the message content from the POST superglobal (you dont want to use REQUEST).
    • The resulting array is then concatenated back into a single string where the concatenating character is a newline.
    • The resulting string is written to the new file.

    In case the above is too hard to follow, here is a version using temp vars and no lambda:

    $allTxtContent = file_get_contents('/path/to/all.txt');
    $numbers = array_filter(str_getcsv($allTxtContent));
    $message = filter_var($_POST['message'], FILTER_SANITIZE_SPECIAL_CHARS);
    $numbersWithMessage = array();
    foreach ($numbers as $number) {
        $numbersWithMessage[] = sprintf('%s %s', trim($number), $message);
    };
    $newString = implode(PHP_EOL, $numbersWithMessage);
    file_put_contents('/path/to/new.txt', $newString);
    

    It does the same thing.

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

Sidebar

Related Questions

This is my first post on Stack, so please bear with me if I
This is my first post in this forum, so please, be patient with me.
This is my first post and I my first experience with jquery. I have
This is my first post on stackoverflow, so please excuse me if my question
This is my first post, thx, you've been very helpful. But I'm stuck. I
This is my first post here. I have a problem. I need to take
This is my first post to stackoverflow, so bear with me. :) I am
this is my first post on this great source of programming information. I have
First, forgive me if this has been answered already. I spent a few hours
This is my first post here so go easy. I am trying to build

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.