Suppose I have some bank spaces in an empty sting, how can I chech the bank spaces and print it is really a empty string.

Thnak you.

    and empty string with spaces, trim($string) will make a blank string with spaces = ""

    $string = " ";
    trim($string) will = ""

      a month later

      Please take a look of the following code. I am getting the output "Different" What kind of string operation I have to do so that I will get "Same". Thank you.

      <?php
      $name1 = "     Hello          World        ");
      $name2 = "Hello World";
      
      print "Before: $name1<br>";
      print "Before: $name2<br>";
      
      if ($name1 == $name2)
      
      print "Same";
      
      else
          print "Different";
      
      ?>
      

        I think the simplest way would be this.

        <?php
        $ary_str[0] = "     Hello                World            ";
        $ary_str[1] = "Hello World";
        
        $i=0;
        foreach ($ary_str as $cur_str) {
          $ary_temp = explode(" ",$cur_str);
          $ary_str[$i] = $cur_str;
          $i++;
        }
        
        if($ary_str[0] == $ary_str[1]) {
          echo "The Flux Capcitator Works.";
        } else {
          echo "No good idea comes from hitting your head on the toilet."
        }
        ?>

          I think there's also:

          function removeExtraSpaces($str) {
          	//remove whitespace from start and end
          	$str = trim($str);
          	//convert whitespace in between to a single space each
          	return preg_replace("/[\\\\n\\\\r\\\\t ]+/", " ", $str);
          }

          then you can simply use this function on your strings.

          I used this myself to prevent spoofing of nicknames since extra whitespace is not shown when a page is displayed.

          EDIT:
          hmm... think there's a bug, so fixed it.

            drawmack,

            You code does not give me the right output.

              laserlight,

              Can you please explain me what's going on in the following trick in your code.

              "/[\n\r\t ]+/"

              Thank you.

                They arent really needed here, since \r and \n are the linefeed and newline characters.

                basically, the [chars] specify a group of characters to parse with.
                The + indicates one or more of such a character belonging to this group.

                  Write a Reply...