removing whitespace from a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • smilesinblues@gmail.com

    removing whitespace from a string?

    Hi,
    Here is the code I use to change whitespace from a string $test to
    hypen -.

    <?Php

    $test="Teststri ng test test hay man test test test test";

    // Chr(32) is ASCI code for whitespace and Chr(45) is for -

    $test = str_replace(Chr (32), Chr(45), $test);

    echo $test;
    ?>

    My problem is I have to take out the whitespace completely form the
    string.

    I will be thankful if anyone can help me, or guide me to the function
    name that can be used for this.

    Thank You for help.

  • Andy Hassall

    #2
    Re: removing whitespace from a string?

    On 5 Mar 2005 04:33:16 -0800, smilesinblues@g mail.com wrote:
    [color=blue]
    >Hi,
    >Here is the code I use to change whitespace from a string $test to
    >hypen -.
    >
    ><?Php
    >
    >$test="Teststr ing test test hay man test test test test";
    >
    >// Chr(32) is ASCI code for whitespace and Chr(45) is for -
    >
    >$test = str_replace(Chr (32), Chr(45), $test);
    >
    >echo $test;
    >?>
    >
    >My problem is I have to take out the whitespace completely form the
    >string.[/color]

    Do exactly the same, but replace it with empty strings.

    $test = str_replace(chr (32), "", $test);

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • smilesinblues@gmail.com

      #3
      Re: removing whitespace from a string?

      Hi,

      Thanks Andy It worked

      thanks a lot

      bye

      Comment

      • Michael Fesser

        #4
        Re: removing whitespace from a string?

        .oO(Andy Hassall)
        [color=blue]
        >Do exactly the same, but replace it with empty strings.
        >
        >$test = str_replace(chr (32), "", $test);[/color]

        Why the chr() function? Why no simply:

        $test = str_replace(' ', '', $test);

        Micha

        Comment

        • Andy Hassall

          #5
          Re: removing whitespace from a string?

          On Sun, 06 Mar 2005 21:19:49 +0100, Michael Fesser <netizen@gmx.ne t> wrote:
          [color=blue]
          > .oO(Andy Hassall)
          >[color=green]
          >>Do exactly the same, but replace it with empty strings.
          >>
          >>$test = str_replace(chr (32), "", $test);[/color]
          >
          >Why the chr() function? Why no simply:
          >
          >$test = str_replace(' ', '', $test);[/color]

          Well, I just copied the OP's code which had the chr function, but how's this;
          if all you want to do is replace the space character chr(32), can you see the
          difference between:

          $test = str_replace(' ', '', $test);
          $test = str_replace(' ', '', $test);

          One of these does not do what you think :-) (Provided it survives the
          copy&paste&post process...)

          --
          Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
          <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

          Comment

          • Michael Fesser

            #6
            Re: removing whitespace from a string?

            .oO(Andy Hassall)
            [color=blue]
            >Well, I just copied the OP's code which had the chr function, but how's this;
            >if all you want to do is replace the space character chr(32), can you see the
            >difference between:
            >
            >$test = str_replace(' ', '', $test);
            >$test = str_replace(' ', '', $test);
            >
            >One of these does not do what you think :-)[/color]

            I knew something like that would come up. ;)

            But I assume the OP knows what he's doing and what he really wants to
            replace. He should be able to hit the right key(s).

            Micha

            Comment

            Working...