Friday, November 25, 2016

how to use trim method in php

                  We use trim($str) function in php to remove all white-spaces from starting and ending of string-data . Characters that we refer to white-space such as (space, tab, new line, carriage return, null-character, vertical-tab). 
                For example,
<html>
       <head>
                <title > Learn Php</title>
      </head>
      <body>

          <?php
                  $str=" I am a Student ";
                  echo '<p>'.'"We have String $str=" I am a Student "'.'</p>';
                 echo '<p>'."Before Trim:".$str,"Mr. Steav";
                echo '<p>'."After Trim:".trim($str),"Mr.Steav";

             ?>
         </body>
</html>

          Moreover, we can remove any characters from starting and ending from string-data by using function trim($str,$anycharacters). For Example, When you want to get any string value without any character at starting and ending of it.

please below:

<html>
          <head>
               <title > Learn Php</title>
          </head>
          <body>

          <?php 
                     $str="I am a m I Student m";
                    echo '<p>'.'When You Want to remove "I am" from "$str"'.'</p>';
                    echo '<p>'.'Before trim:'.$str."at RUPP".'</p>';
                    echo '<p>'.'After trim:'.trim($str,"I am")."at RUPP".'</p>';

            ?>
         </body>
</html>

            In example above  function trim will remove any character such as (I,space,a,m) from starting and ending of $str.

Please see result:









No comments:

Post a Comment