Recursive trim() function to handle arrays and objects











up vote
1
down vote

favorite












I have come up with a recursive function which applies trim() recursively to all string members of an array/object.



I don't have any specific quandaries about my code so I am receptive to all feedback.



My goal is maximum compatibility for all PHP versions: past, present, and future.



<?php
/**
* trim_r
*
* Recursively trim an array's or object's string values
* Preserves all non-string values
*
* @access public
* @param mixed
* @param mixed
* @return mixed
*/
function &trim_r( &$o, $character_mask = null )
{
// Only apply trim() to strings
if( is_string( $o ) )
{
// Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
$o = ( $character_mask === null ? trim( $o ) : trim( $o, $character_mask ) );
}
elseif( is_array( $o ) || is_object( $o ) )
{
// Loop this array/object and apply trim_r() to its members
foreach( $o as &$v )
{
trim_r( $v );
}
}

// Supply this just in case the invoker wishes to receive result as a reference
return $o;
}









share|improve this question




























    up vote
    1
    down vote

    favorite












    I have come up with a recursive function which applies trim() recursively to all string members of an array/object.



    I don't have any specific quandaries about my code so I am receptive to all feedback.



    My goal is maximum compatibility for all PHP versions: past, present, and future.



    <?php
    /**
    * trim_r
    *
    * Recursively trim an array's or object's string values
    * Preserves all non-string values
    *
    * @access public
    * @param mixed
    * @param mixed
    * @return mixed
    */
    function &trim_r( &$o, $character_mask = null )
    {
    // Only apply trim() to strings
    if( is_string( $o ) )
    {
    // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
    $o = ( $character_mask === null ? trim( $o ) : trim( $o, $character_mask ) );
    }
    elseif( is_array( $o ) || is_object( $o ) )
    {
    // Loop this array/object and apply trim_r() to its members
    foreach( $o as &$v )
    {
    trim_r( $v );
    }
    }

    // Supply this just in case the invoker wishes to receive result as a reference
    return $o;
    }









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have come up with a recursive function which applies trim() recursively to all string members of an array/object.



      I don't have any specific quandaries about my code so I am receptive to all feedback.



      My goal is maximum compatibility for all PHP versions: past, present, and future.



      <?php
      /**
      * trim_r
      *
      * Recursively trim an array's or object's string values
      * Preserves all non-string values
      *
      * @access public
      * @param mixed
      * @param mixed
      * @return mixed
      */
      function &trim_r( &$o, $character_mask = null )
      {
      // Only apply trim() to strings
      if( is_string( $o ) )
      {
      // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
      $o = ( $character_mask === null ? trim( $o ) : trim( $o, $character_mask ) );
      }
      elseif( is_array( $o ) || is_object( $o ) )
      {
      // Loop this array/object and apply trim_r() to its members
      foreach( $o as &$v )
      {
      trim_r( $v );
      }
      }

      // Supply this just in case the invoker wishes to receive result as a reference
      return $o;
      }









      share|improve this question















      I have come up with a recursive function which applies trim() recursively to all string members of an array/object.



      I don't have any specific quandaries about my code so I am receptive to all feedback.



      My goal is maximum compatibility for all PHP versions: past, present, and future.



      <?php
      /**
      * trim_r
      *
      * Recursively trim an array's or object's string values
      * Preserves all non-string values
      *
      * @access public
      * @param mixed
      * @param mixed
      * @return mixed
      */
      function &trim_r( &$o, $character_mask = null )
      {
      // Only apply trim() to strings
      if( is_string( $o ) )
      {
      // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
      $o = ( $character_mask === null ? trim( $o ) : trim( $o, $character_mask ) );
      }
      elseif( is_array( $o ) || is_object( $o ) )
      {
      // Loop this array/object and apply trim_r() to its members
      foreach( $o as &$v )
      {
      trim_r( $v );
      }
      }

      // Supply this just in case the invoker wishes to receive result as a reference
      return $o;
      }






      php strings array recursion






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 16:54









      200_success

      127k15148410




      127k15148410










      asked Nov 13 at 16:04









      MonkeyZeus

      2481315




      2481315






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          Avoid if-else nesting to keep things flat. Return inside the first if is enough to do so.





          I added is_iterable() so this function can handle more types than just array



          /**
          * trim_r
          *
          * Recursively trim an array's or object's string values
          * Preserves all non-string values
          *
          * @access public
          * @param mixed
          * @param mixed
          * @return mixed
          */
          function &trim_r( &$o, $character_mask = null )
          {

          // Only apply trim() to strings
          if( is_string( $o ) )
          {
          // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
          $o = trim( $o, $character_mask ?? " tnrx0B" );
          return $o;
          }

          if( is_iterable($o) || is_object( $o ) )
          {
          // Loop this array/object and apply trim_r() to its members
          foreach( $o as &$v )
          {
          trim_r( $v );
          }

          }

          // Supply this just in case the invoker wishes to receive result as a reference
          return $o;
          }





          share|improve this answer










          New contributor




          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Hmm, I like this but I should have specified maximum compatibility for as many PHP versions as possible. The null coalescing operator was introduced in PHP 7. As for " tnrx0B", I am hesitant to hardcode this because there is a chance it will change in the future just like the encoding parameter from htmlentities()
            – MonkeyZeus
            Nov 13 at 16:41












          • Sorry but is_iterable() also fails to meet the compatibility criteria :-/
            – MonkeyZeus
            Nov 13 at 17:54










          • @MonkeyZeus right, I wasn't aware these is such criteria at the time of writing.
            – nforced
            2 days ago










          • I know, that's why I upvoted you anyways
            – MonkeyZeus
            yesterday











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f207570%2frecursive-trim-function-to-handle-arrays-and-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          Avoid if-else nesting to keep things flat. Return inside the first if is enough to do so.





          I added is_iterable() so this function can handle more types than just array



          /**
          * trim_r
          *
          * Recursively trim an array's or object's string values
          * Preserves all non-string values
          *
          * @access public
          * @param mixed
          * @param mixed
          * @return mixed
          */
          function &trim_r( &$o, $character_mask = null )
          {

          // Only apply trim() to strings
          if( is_string( $o ) )
          {
          // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
          $o = trim( $o, $character_mask ?? " tnrx0B" );
          return $o;
          }

          if( is_iterable($o) || is_object( $o ) )
          {
          // Loop this array/object and apply trim_r() to its members
          foreach( $o as &$v )
          {
          trim_r( $v );
          }

          }

          // Supply this just in case the invoker wishes to receive result as a reference
          return $o;
          }





          share|improve this answer










          New contributor




          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Hmm, I like this but I should have specified maximum compatibility for as many PHP versions as possible. The null coalescing operator was introduced in PHP 7. As for " tnrx0B", I am hesitant to hardcode this because there is a chance it will change in the future just like the encoding parameter from htmlentities()
            – MonkeyZeus
            Nov 13 at 16:41












          • Sorry but is_iterable() also fails to meet the compatibility criteria :-/
            – MonkeyZeus
            Nov 13 at 17:54










          • @MonkeyZeus right, I wasn't aware these is such criteria at the time of writing.
            – nforced
            2 days ago










          • I know, that's why I upvoted you anyways
            – MonkeyZeus
            yesterday















          up vote
          2
          down vote













          Avoid if-else nesting to keep things flat. Return inside the first if is enough to do so.





          I added is_iterable() so this function can handle more types than just array



          /**
          * trim_r
          *
          * Recursively trim an array's or object's string values
          * Preserves all non-string values
          *
          * @access public
          * @param mixed
          * @param mixed
          * @return mixed
          */
          function &trim_r( &$o, $character_mask = null )
          {

          // Only apply trim() to strings
          if( is_string( $o ) )
          {
          // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
          $o = trim( $o, $character_mask ?? " tnrx0B" );
          return $o;
          }

          if( is_iterable($o) || is_object( $o ) )
          {
          // Loop this array/object and apply trim_r() to its members
          foreach( $o as &$v )
          {
          trim_r( $v );
          }

          }

          // Supply this just in case the invoker wishes to receive result as a reference
          return $o;
          }





          share|improve this answer










          New contributor




          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Hmm, I like this but I should have specified maximum compatibility for as many PHP versions as possible. The null coalescing operator was introduced in PHP 7. As for " tnrx0B", I am hesitant to hardcode this because there is a chance it will change in the future just like the encoding parameter from htmlentities()
            – MonkeyZeus
            Nov 13 at 16:41












          • Sorry but is_iterable() also fails to meet the compatibility criteria :-/
            – MonkeyZeus
            Nov 13 at 17:54










          • @MonkeyZeus right, I wasn't aware these is such criteria at the time of writing.
            – nforced
            2 days ago










          • I know, that's why I upvoted you anyways
            – MonkeyZeus
            yesterday













          up vote
          2
          down vote










          up vote
          2
          down vote









          Avoid if-else nesting to keep things flat. Return inside the first if is enough to do so.





          I added is_iterable() so this function can handle more types than just array



          /**
          * trim_r
          *
          * Recursively trim an array's or object's string values
          * Preserves all non-string values
          *
          * @access public
          * @param mixed
          * @param mixed
          * @return mixed
          */
          function &trim_r( &$o, $character_mask = null )
          {

          // Only apply trim() to strings
          if( is_string( $o ) )
          {
          // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
          $o = trim( $o, $character_mask ?? " tnrx0B" );
          return $o;
          }

          if( is_iterable($o) || is_object( $o ) )
          {
          // Loop this array/object and apply trim_r() to its members
          foreach( $o as &$v )
          {
          trim_r( $v );
          }

          }

          // Supply this just in case the invoker wishes to receive result as a reference
          return $o;
          }





          share|improve this answer










          New contributor




          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          Avoid if-else nesting to keep things flat. Return inside the first if is enough to do so.





          I added is_iterable() so this function can handle more types than just array



          /**
          * trim_r
          *
          * Recursively trim an array's or object's string values
          * Preserves all non-string values
          *
          * @access public
          * @param mixed
          * @param mixed
          * @return mixed
          */
          function &trim_r( &$o, $character_mask = null )
          {

          // Only apply trim() to strings
          if( is_string( $o ) )
          {
          // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
          $o = trim( $o, $character_mask ?? " tnrx0B" );
          return $o;
          }

          if( is_iterable($o) || is_object( $o ) )
          {
          // Loop this array/object and apply trim_r() to its members
          foreach( $o as &$v )
          {
          trim_r( $v );
          }

          }

          // Supply this just in case the invoker wishes to receive result as a reference
          return $o;
          }






          share|improve this answer










          New contributor




          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited Nov 13 at 17:04





















          New contributor




          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 13 at 16:36









          nforced

          414




          414




          New contributor




          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          nforced is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • Hmm, I like this but I should have specified maximum compatibility for as many PHP versions as possible. The null coalescing operator was introduced in PHP 7. As for " tnrx0B", I am hesitant to hardcode this because there is a chance it will change in the future just like the encoding parameter from htmlentities()
            – MonkeyZeus
            Nov 13 at 16:41












          • Sorry but is_iterable() also fails to meet the compatibility criteria :-/
            – MonkeyZeus
            Nov 13 at 17:54










          • @MonkeyZeus right, I wasn't aware these is such criteria at the time of writing.
            – nforced
            2 days ago










          • I know, that's why I upvoted you anyways
            – MonkeyZeus
            yesterday


















          • Hmm, I like this but I should have specified maximum compatibility for as many PHP versions as possible. The null coalescing operator was introduced in PHP 7. As for " tnrx0B", I am hesitant to hardcode this because there is a chance it will change in the future just like the encoding parameter from htmlentities()
            – MonkeyZeus
            Nov 13 at 16:41












          • Sorry but is_iterable() also fails to meet the compatibility criteria :-/
            – MonkeyZeus
            Nov 13 at 17:54










          • @MonkeyZeus right, I wasn't aware these is such criteria at the time of writing.
            – nforced
            2 days ago










          • I know, that's why I upvoted you anyways
            – MonkeyZeus
            yesterday
















          Hmm, I like this but I should have specified maximum compatibility for as many PHP versions as possible. The null coalescing operator was introduced in PHP 7. As for " tnrx0B", I am hesitant to hardcode this because there is a chance it will change in the future just like the encoding parameter from htmlentities()
          – MonkeyZeus
          Nov 13 at 16:41






          Hmm, I like this but I should have specified maximum compatibility for as many PHP versions as possible. The null coalescing operator was introduced in PHP 7. As for " tnrx0B", I am hesitant to hardcode this because there is a chance it will change in the future just like the encoding parameter from htmlentities()
          – MonkeyZeus
          Nov 13 at 16:41














          Sorry but is_iterable() also fails to meet the compatibility criteria :-/
          – MonkeyZeus
          Nov 13 at 17:54




          Sorry but is_iterable() also fails to meet the compatibility criteria :-/
          – MonkeyZeus
          Nov 13 at 17:54












          @MonkeyZeus right, I wasn't aware these is such criteria at the time of writing.
          – nforced
          2 days ago




          @MonkeyZeus right, I wasn't aware these is such criteria at the time of writing.
          – nforced
          2 days ago












          I know, that's why I upvoted you anyways
          – MonkeyZeus
          yesterday




          I know, that's why I upvoted you anyways
          – MonkeyZeus
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207570%2frecursive-trim-function-to-handle-arrays-and-objects%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

          Morgemoulin

          Scott Moir

          Souastre