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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:04:16+00:00 2026-06-16T13:04:16+00:00

I am relatively new with php and am in need of some help. I

  • 0

I am relatively new with php and am in need of some help.

I have a html form, when the user can submit the text. This text is processed by php, and if there is a line with more than 20 characters, happens a line break.
After, all text is “exploded”, and I get an array. So far all is well.

function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;}

    $format = $_POST['format'];
    $title= strtoupper($_POST['titre']);
    $txtcontent = $_POST['texte'];
    $txtcontent =  wordwrap($txtcontent,20,hexToStr('0D0A'),true);
    $txtcontent = explode("\n", $txtcontent);

print_r(array_chunk($txtcontent, 9, false));



$filecontent = implode(array_chunk($txtcontent, 9, false)); 

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename='.$_POST['titre'].'.'.$format.'');
header('Cache-Control: max-age=0');

//$fh = fopen($filename, 'wb');
$fh = fopen('php://output', 'wb');
fwrite($fh, $filecontent);
fclose($fh);

Im using array_chunk to separate the array $txtcontent in arrays chunk with 9 elements.

Now, I need add some constant text at the beginning and end of each new array element, according to the key value.
Maybe i need to do a loop or something like it, I’ve tried many times and still can not.
I need help to do this.

Exemple:
If i submit this text in the web form:

line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12

i will have:

Array ( [0] => Array ( [0] => line1 [1] => line2 [2] => line3 [3] => line4 [4] => line5 [5] => line6 [6] => line7 [7] => line8 [8] => line9 ) [1] => Array ( [0] => line10 [1] => line11 [2] => line12 ) ) 

This is where I need help:

After process it (after adding constant text, according to the key value), it should be something like it. However i dont know how i can do it.

Array ( [0] => Array ( 
[0] => \Text 1,1,"line1"
[1] => \Text 7,1,"line2"
[2] => \Text 13,1,"line3"
[3] => \Text 19,1,"line4"
[4] => \Text 25,1,"line5"
[5] => \Text 31,1,"line6"
[6] => \Text 37,1,"line7"
[7] => \Text 43,1,"line8"
[8] => \Text 49,1,"line9"
) [1] => Array ( 
[0] => \Text 1,1,"line10"
[1] => \Text 7,1,"line11"
[2] => \Text 13,1,"line12"
) ) 

Finnaly, when i output it to the file, I shall have:

(I dont know how to output array_chunk, i also need help to do this.)

\Text 1,1,"line1"
\Text 7,1,"line2"
\Text 13,1,"line3"
\Text 19,1,"line4"
\Text 25,1,"line5"
\Text 31,1,"line6"
\Text 37,1,"line7"
\Text 43,1,"line8"
\Text 49,1,"line9"
\Text 1,1,"line10"
\Text 7,1,"line11"
\Text 13,1,"line12"

Note that the element numbers in array may be larger or smaller, this is only a exemple.

Note also that arrays with same key (exemple: line 1 and 10) will have the same extra text at the end and beggin.

I think this question is quite hard, but I hope someone can help me. I’m already a week ago trying to solve it and I still can not.

  • 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-16T13:04:17+00:00Added an answer on June 16, 2026 at 1:04 pm

    I hope this helps, but this question is a lot to take in:

    You can do a foreach loop on the multidimensional array you make with array_chunk, and add the text that you want to add like this:

    $chunkedarray = array_chunk($txtcontent, 9, false);
    foreach($chunkedarray as $key => $array){
        foreach($array as $k => $v){
            $chunkedarray[$key][$k] = '\text '.($k*6+1).',1,'.$v;
        }
    }
    

    Then you can flatten the multi dimensional array with a function I found from @alienwebguy on this stackoverflow question:

    function array_flatten($array) { 
      if (!is_array($array)) { 
        return FALSE; 
      } 
      $result = array(); 
      foreach ($array as $key => $value) { 
        if (is_array($value)) { 
          $result = array_merge($result, array_flatten($value)); 
        } 
        else { 
          $result[$key] = $value; 
        } 
      } 
      return $result; 
    } 
    $flattenedarray = array_flatten($chunkedarray);
    

    Then you can output the flattened multidimensional array with another for loop:

    foreach($flattenedarray as $v){
      echo $v.'<br>';
    }
    

    Check out the example code on IDEone.com: http://ideone.com/vzPJOB

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

Sidebar

Related Questions

This may be a silly question, but as someone relatively new to PHP, I'm
I'm relatively new at PHP and came across a slight problem. I have a
Relatively new to PHP here, but here's where I'm at: I have a simple
I am relatively new to PHP/MySQL and have been having a problem that I
This is the code to grab tweets, but i need this in PHP, can
I'm relatively new with PHP and more so with XML. I have a script
I am relatively new to PHP. I have drilled down a couple of levels
I'm relatively new to php / mysqli i'm having trouble authenticating the user, I'm
Well I'm relatively new to the use of PHP arrays and I have the
I am relatively new to PHP and having some decent success however I am

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.