PHP Create a string with predefined column and width












2















I have a standard data like this:



+------------+----------+-------+
| name | position | width |
+------------+----------+-------+
| REFERENCE | 20 | 9 |
| TRNSXN | 48 | 14 |
| ESTIM_DATE | 64 | 8 |
+------------+----------+-------+
3 rows in set (0.00 sec)


My goal is to create a string where I can define the position and the width of each element, something like this



                   ETL                         E7954           20181123


As you can see
first string is REFERENCE = "ETL"; starts on 20 and has a maximum 9 characters. if it has less, put space at the rest.



Then second string is TRNSXN = E7954; is start on 48 and have maximum 14 characters, if it has less, put space at the rest. So on, so on.



So far, this is my code:



private function preFormattedText($string, $width)
{
return str_pad($string, $width, " ", STR_PAD_RIGHT);
}

public function actionHermesAnsi($id)
{
$modelWestims =getDataAsAboveDescription ;


$stringWestim = null;
$stringWestim .= $this->preFormattedText('ETL', (int)$modelWestims[0]['width']);
$stringWestim .= $this->preFormattedText('E7954', (int)$modelWestims[1]['width']);

header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=westim.txt");

print $stringWestim;
exit();
}


I got ETL E7954
I can not define the position of it. Please help.










share|improve this question





























    2















    I have a standard data like this:



    +------------+----------+-------+
    | name | position | width |
    +------------+----------+-------+
    | REFERENCE | 20 | 9 |
    | TRNSXN | 48 | 14 |
    | ESTIM_DATE | 64 | 8 |
    +------------+----------+-------+
    3 rows in set (0.00 sec)


    My goal is to create a string where I can define the position and the width of each element, something like this



                       ETL                         E7954           20181123


    As you can see
    first string is REFERENCE = "ETL"; starts on 20 and has a maximum 9 characters. if it has less, put space at the rest.



    Then second string is TRNSXN = E7954; is start on 48 and have maximum 14 characters, if it has less, put space at the rest. So on, so on.



    So far, this is my code:



    private function preFormattedText($string, $width)
    {
    return str_pad($string, $width, " ", STR_PAD_RIGHT);
    }

    public function actionHermesAnsi($id)
    {
    $modelWestims =getDataAsAboveDescription ;


    $stringWestim = null;
    $stringWestim .= $this->preFormattedText('ETL', (int)$modelWestims[0]['width']);
    $stringWestim .= $this->preFormattedText('E7954', (int)$modelWestims[1]['width']);

    header("Content-type: text/plain");
    header("Content-Disposition: attachment; filename=westim.txt");

    print $stringWestim;
    exit();
    }


    I got ETL E7954
    I can not define the position of it. Please help.










    share|improve this question



























      2












      2








      2








      I have a standard data like this:



      +------------+----------+-------+
      | name | position | width |
      +------------+----------+-------+
      | REFERENCE | 20 | 9 |
      | TRNSXN | 48 | 14 |
      | ESTIM_DATE | 64 | 8 |
      +------------+----------+-------+
      3 rows in set (0.00 sec)


      My goal is to create a string where I can define the position and the width of each element, something like this



                         ETL                         E7954           20181123


      As you can see
      first string is REFERENCE = "ETL"; starts on 20 and has a maximum 9 characters. if it has less, put space at the rest.



      Then second string is TRNSXN = E7954; is start on 48 and have maximum 14 characters, if it has less, put space at the rest. So on, so on.



      So far, this is my code:



      private function preFormattedText($string, $width)
      {
      return str_pad($string, $width, " ", STR_PAD_RIGHT);
      }

      public function actionHermesAnsi($id)
      {
      $modelWestims =getDataAsAboveDescription ;


      $stringWestim = null;
      $stringWestim .= $this->preFormattedText('ETL', (int)$modelWestims[0]['width']);
      $stringWestim .= $this->preFormattedText('E7954', (int)$modelWestims[1]['width']);

      header("Content-type: text/plain");
      header("Content-Disposition: attachment; filename=westim.txt");

      print $stringWestim;
      exit();
      }


      I got ETL E7954
      I can not define the position of it. Please help.










      share|improve this question
















      I have a standard data like this:



      +------------+----------+-------+
      | name | position | width |
      +------------+----------+-------+
      | REFERENCE | 20 | 9 |
      | TRNSXN | 48 | 14 |
      | ESTIM_DATE | 64 | 8 |
      +------------+----------+-------+
      3 rows in set (0.00 sec)


      My goal is to create a string where I can define the position and the width of each element, something like this



                         ETL                         E7954           20181123


      As you can see
      first string is REFERENCE = "ETL"; starts on 20 and has a maximum 9 characters. if it has less, put space at the rest.



      Then second string is TRNSXN = E7954; is start on 48 and have maximum 14 characters, if it has less, put space at the rest. So on, so on.



      So far, this is my code:



      private function preFormattedText($string, $width)
      {
      return str_pad($string, $width, " ", STR_PAD_RIGHT);
      }

      public function actionHermesAnsi($id)
      {
      $modelWestims =getDataAsAboveDescription ;


      $stringWestim = null;
      $stringWestim .= $this->preFormattedText('ETL', (int)$modelWestims[0]['width']);
      $stringWestim .= $this->preFormattedText('E7954', (int)$modelWestims[1]['width']);

      header("Content-type: text/plain");
      header("Content-Disposition: attachment; filename=westim.txt");

      print $stringWestim;
      exit();
      }


      I got ETL E7954
      I can not define the position of it. Please help.







      php text






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 8:23









      Nick

      37.2k132443




      37.2k132443










      asked Nov 23 '18 at 8:07









      Fadly DzilFadly Dzil

      8381540




      8381540
























          3 Answers
          3






          active

          oldest

          votes


















          2














          Something like this will do what you want:



          function formatStrings($strings, $positions) {
          $position = 0;
          foreach ($strings as $key => $str) {
          // do we need to move to a new position to display the next string?
          if ($positions[$key]['position'] > $position) {
          echo str_pad('', $positions[$key]['position'] - $position - 1);
          $position = $positions[$key]['position'];
          }
          // display the string
          echo str_pad($str, $positions[$key]['width']);
          $position += $positions[$key]['width'] - 1;
          }
          }

          $strings = array('ETL','E7954','20181123');
          $positions = array(array('position' => 20, 'width' => 9),
          array('position' => 48, 'width' => 14),
          array('position' => 64, 'width' => 8));
          echo formatStrings($strings, $positions);


          Demo on 3v4l.org






          share|improve this answer































            1














            Maybe you can use STR_PAD_LEFT with the position like you did with the width, and remove the length of $stringWestim :



            private function preFormattedText($stringWestim, $string, $width, $position)
            {
            $string = str_pad($string, $position - strlen($stringWestim), " ", STR_PAD_LEFT );
            return str_pad($string, $width, " ", STR_PAD_RIGHT);
            }





            share|improve this answer

































              0














              Thanks for @Nesku and @Nick.



              I use Nick's answer, a little modification based my needs like this:



              private function formatStrings($strings, $positions)
              {
              $position = 0;
              $returnString = null;
              foreach ($strings as $key => $str) {
              // do we need to move to a new position to display the next string?
              if ($positions[$key]['position'] > $position) {
              $returnString .= str_pad('', $positions[$key]['position'] - $position - 1);
              $position = $positions[$key]['position'];
              }
              // display the string
              $returnString .= str_pad($str, $positions[$key]['width']);
              $position += $positions[$key]['width'] - 1;
              }

              return $returnString;
              }





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442769%2fphp-create-a-string-with-predefined-column-and-width%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                Something like this will do what you want:



                function formatStrings($strings, $positions) {
                $position = 0;
                foreach ($strings as $key => $str) {
                // do we need to move to a new position to display the next string?
                if ($positions[$key]['position'] > $position) {
                echo str_pad('', $positions[$key]['position'] - $position - 1);
                $position = $positions[$key]['position'];
                }
                // display the string
                echo str_pad($str, $positions[$key]['width']);
                $position += $positions[$key]['width'] - 1;
                }
                }

                $strings = array('ETL','E7954','20181123');
                $positions = array(array('position' => 20, 'width' => 9),
                array('position' => 48, 'width' => 14),
                array('position' => 64, 'width' => 8));
                echo formatStrings($strings, $positions);


                Demo on 3v4l.org






                share|improve this answer




























                  2














                  Something like this will do what you want:



                  function formatStrings($strings, $positions) {
                  $position = 0;
                  foreach ($strings as $key => $str) {
                  // do we need to move to a new position to display the next string?
                  if ($positions[$key]['position'] > $position) {
                  echo str_pad('', $positions[$key]['position'] - $position - 1);
                  $position = $positions[$key]['position'];
                  }
                  // display the string
                  echo str_pad($str, $positions[$key]['width']);
                  $position += $positions[$key]['width'] - 1;
                  }
                  }

                  $strings = array('ETL','E7954','20181123');
                  $positions = array(array('position' => 20, 'width' => 9),
                  array('position' => 48, 'width' => 14),
                  array('position' => 64, 'width' => 8));
                  echo formatStrings($strings, $positions);


                  Demo on 3v4l.org






                  share|improve this answer


























                    2












                    2








                    2







                    Something like this will do what you want:



                    function formatStrings($strings, $positions) {
                    $position = 0;
                    foreach ($strings as $key => $str) {
                    // do we need to move to a new position to display the next string?
                    if ($positions[$key]['position'] > $position) {
                    echo str_pad('', $positions[$key]['position'] - $position - 1);
                    $position = $positions[$key]['position'];
                    }
                    // display the string
                    echo str_pad($str, $positions[$key]['width']);
                    $position += $positions[$key]['width'] - 1;
                    }
                    }

                    $strings = array('ETL','E7954','20181123');
                    $positions = array(array('position' => 20, 'width' => 9),
                    array('position' => 48, 'width' => 14),
                    array('position' => 64, 'width' => 8));
                    echo formatStrings($strings, $positions);


                    Demo on 3v4l.org






                    share|improve this answer













                    Something like this will do what you want:



                    function formatStrings($strings, $positions) {
                    $position = 0;
                    foreach ($strings as $key => $str) {
                    // do we need to move to a new position to display the next string?
                    if ($positions[$key]['position'] > $position) {
                    echo str_pad('', $positions[$key]['position'] - $position - 1);
                    $position = $positions[$key]['position'];
                    }
                    // display the string
                    echo str_pad($str, $positions[$key]['width']);
                    $position += $positions[$key]['width'] - 1;
                    }
                    }

                    $strings = array('ETL','E7954','20181123');
                    $positions = array(array('position' => 20, 'width' => 9),
                    array('position' => 48, 'width' => 14),
                    array('position' => 64, 'width' => 8));
                    echo formatStrings($strings, $positions);


                    Demo on 3v4l.org







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 8:19









                    NickNick

                    37.2k132443




                    37.2k132443

























                        1














                        Maybe you can use STR_PAD_LEFT with the position like you did with the width, and remove the length of $stringWestim :



                        private function preFormattedText($stringWestim, $string, $width, $position)
                        {
                        $string = str_pad($string, $position - strlen($stringWestim), " ", STR_PAD_LEFT );
                        return str_pad($string, $width, " ", STR_PAD_RIGHT);
                        }





                        share|improve this answer






























                          1














                          Maybe you can use STR_PAD_LEFT with the position like you did with the width, and remove the length of $stringWestim :



                          private function preFormattedText($stringWestim, $string, $width, $position)
                          {
                          $string = str_pad($string, $position - strlen($stringWestim), " ", STR_PAD_LEFT );
                          return str_pad($string, $width, " ", STR_PAD_RIGHT);
                          }





                          share|improve this answer




























                            1












                            1








                            1







                            Maybe you can use STR_PAD_LEFT with the position like you did with the width, and remove the length of $stringWestim :



                            private function preFormattedText($stringWestim, $string, $width, $position)
                            {
                            $string = str_pad($string, $position - strlen($stringWestim), " ", STR_PAD_LEFT );
                            return str_pad($string, $width, " ", STR_PAD_RIGHT);
                            }





                            share|improve this answer















                            Maybe you can use STR_PAD_LEFT with the position like you did with the width, and remove the length of $stringWestim :



                            private function preFormattedText($stringWestim, $string, $width, $position)
                            {
                            $string = str_pad($string, $position - strlen($stringWestim), " ", STR_PAD_LEFT );
                            return str_pad($string, $width, " ", STR_PAD_RIGHT);
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 23 '18 at 8:50

























                            answered Nov 23 '18 at 8:16









                            NeskuNesku

                            4231412




                            4231412























                                0














                                Thanks for @Nesku and @Nick.



                                I use Nick's answer, a little modification based my needs like this:



                                private function formatStrings($strings, $positions)
                                {
                                $position = 0;
                                $returnString = null;
                                foreach ($strings as $key => $str) {
                                // do we need to move to a new position to display the next string?
                                if ($positions[$key]['position'] > $position) {
                                $returnString .= str_pad('', $positions[$key]['position'] - $position - 1);
                                $position = $positions[$key]['position'];
                                }
                                // display the string
                                $returnString .= str_pad($str, $positions[$key]['width']);
                                $position += $positions[$key]['width'] - 1;
                                }

                                return $returnString;
                                }





                                share|improve this answer




























                                  0














                                  Thanks for @Nesku and @Nick.



                                  I use Nick's answer, a little modification based my needs like this:



                                  private function formatStrings($strings, $positions)
                                  {
                                  $position = 0;
                                  $returnString = null;
                                  foreach ($strings as $key => $str) {
                                  // do we need to move to a new position to display the next string?
                                  if ($positions[$key]['position'] > $position) {
                                  $returnString .= str_pad('', $positions[$key]['position'] - $position - 1);
                                  $position = $positions[$key]['position'];
                                  }
                                  // display the string
                                  $returnString .= str_pad($str, $positions[$key]['width']);
                                  $position += $positions[$key]['width'] - 1;
                                  }

                                  return $returnString;
                                  }





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Thanks for @Nesku and @Nick.



                                    I use Nick's answer, a little modification based my needs like this:



                                    private function formatStrings($strings, $positions)
                                    {
                                    $position = 0;
                                    $returnString = null;
                                    foreach ($strings as $key => $str) {
                                    // do we need to move to a new position to display the next string?
                                    if ($positions[$key]['position'] > $position) {
                                    $returnString .= str_pad('', $positions[$key]['position'] - $position - 1);
                                    $position = $positions[$key]['position'];
                                    }
                                    // display the string
                                    $returnString .= str_pad($str, $positions[$key]['width']);
                                    $position += $positions[$key]['width'] - 1;
                                    }

                                    return $returnString;
                                    }





                                    share|improve this answer













                                    Thanks for @Nesku and @Nick.



                                    I use Nick's answer, a little modification based my needs like this:



                                    private function formatStrings($strings, $positions)
                                    {
                                    $position = 0;
                                    $returnString = null;
                                    foreach ($strings as $key => $str) {
                                    // do we need to move to a new position to display the next string?
                                    if ($positions[$key]['position'] > $position) {
                                    $returnString .= str_pad('', $positions[$key]['position'] - $position - 1);
                                    $position = $positions[$key]['position'];
                                    }
                                    // display the string
                                    $returnString .= str_pad($str, $positions[$key]['width']);
                                    $position += $positions[$key]['width'] - 1;
                                    }

                                    return $returnString;
                                    }






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 23 '18 at 9:02









                                    Fadly DzilFadly Dzil

                                    8381540




                                    8381540






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442769%2fphp-create-a-string-with-predefined-column-and-width%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                        Alcedinidae

                                        RAC Tourist Trophy