hi,

I have two very simple questions b'coz I have stuck in this :

1.> I have written a message as;
$msg="statement1
statement2
statement3";
How can I let the statement2 to be on the nextline after statement1.

2.>
If the user has entered his name in the input box as:
"chandan rishiraj tiwari"
Which function do i need to use such that when i calculate the length of string, it should not calculate the spaces between them.

Please suggest me ?

Thanking You!

    You could strip off all the spaces, then use strlen(). If you need the original string, store the result in a temporary.

      1.) for html output use

      print nl2br($msg);

      plain text: should already be accomplished by the way you wrote it (\n would be the line break character if you had to have all statements in one line)

      2.) just an example:

      $msg = "chandan rishiraj tiwari";
      print len_wo_spaces($msg);
      
      function $len_wo_spaces($input) {
          $token = explode(" ", $input);
      
      foreach ($token AS $key => $val) {
          $len_wo_spaces += strlen($val);
      }
      
      return $len_wo_spaces;
      }
      
      

      or, as laserlight suggested:

      $msg = "chandan rishiraj tiwari";
      print strlen(len_wo_spaces($msg));
      
      function $len_wo_spaces($input) {
          for ($i = 0; $i < strlen($input); $i++) {
              if ($input{$i} != " ") {
                  $str_wo_spaces += $input{$i};
              }
          }
      
      return $str_wo_spaces;
      }
      
      

      or, if you dont need the temp string:

      $msg = "chandan rishiraj tiwari";
      print len_wo_spaces($msg);
      
      function $len_wo_spaces($input) {
          for ($i = 0; $i < strlen($input); $i++) {
              if ($input{$i} != " ") {
                  $str_wo_spaces++;
              }
          }
      
      return $str_wo_spaces;
      }
      
      

        and of course, these were onlt 3 orf the procedural ways to do that ;-)

          I would go a bit simplier...

          $msg = "chandan rishiraj tiwari"; 
          $intLength = strlen(str_replace(array(' ', '	', "\n", "\r"), '', $msg));
          echo 'The length of "' . $msg . '" without spaces, tabs, new lines of return carriage is : ' . $strLength;
          

          Or

          $msg = "chandan rishiraj tiwari"; 
          $intLength = strlen(preg_replace('/[\s]/', '', $msg));
          echo 'The length of "' . $msg . '" without spacing characters (according to PREG) is : ' . $strLength;
          

            (Oh, another "how many ways of writing this are there?" thing)

            Or

            $counts = count_chars($string, 1);
            unset($counts[ord(' ')]);
            $counted_chars = array_sum($counts);
            
              Write a Reply...