Delete multiple spaces and special characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mindy Geac

    Delete multiple spaces and special characters

    Is it possible to delete special caracters from a string and multiple
    spaces?

    When the input is : "a&*bbb cc/c d!d"
    I want the result : "abbb ccc dd"

    thnx.
    MJ

    <?php

    $q = $_REQUEST['q'];
    $q = strip_tags($q);

    echo $q

    ?>







  • John Dunlop

    #2
    Re: Delete multiple spaces and special characters

    Mindy Geac wrote:
    [color=blue]
    > Is it possible to delete special caracters from a string and multiple
    > spaces?
    >
    > When the input is : "a&*bbb cc/c d!d"
    > I want the result : "abbb ccc dd"[/color]

    preg_replace(
    array('` {2,}`',"`[$specials]+`"),
    array(' ',''),
    $subject)

    --
    Jock

    Comment

    • kerul4u

      #3
      Re: Delete multiple spaces and special characters

      Try this simple yet powerfull solution

      //Your String
      $string = "a&*bbb cc/c d!d";

      //Array of special charecters you want to replace
      $special = array('/','!','&','*'); //here you can add as many char. you
      want
      $replacements = "";

      echo str_replace($sp ecial,'',$strin g);

      KERUL
      [ProDesignZ]

      Comment

      • Kimmo Laine

        #4
        Re: Delete multiple spaces and special characters

        "kerul4u" <kerul4u@gmail. com> wrote in message
        news:1125318171 .067211.108700@ z14g2000cwz.goo glegroups.com.. .[color=blue]
        > Try this simple yet powerfull solution
        >
        > //Your String
        > $string = "a&*bbb cc/c d!d";
        >
        > //Array of special charecters you want to replace
        > $special = array('/','!','&','*'); //here you can add as many char. you
        > want
        > $replacements = "";
        >
        > echo str_replace($sp ecial,'',$strin g);
        >[/color]


        That still doesn't remove multiple spaces, ie. whitespace. Some regexp
        wizard kid could tell how the whitespace is replaced with a single space.
        I'd say that converting the spcial chars would be easy too with regular
        expressions.

        The thing is I'm all thumbs with regexp so I can only recommend using it but
        I have no idea how it would work ;)

        --
        Welcome to Usenet! Please leave tolerance, understanding
        and intelligence at the door. They aren't welcome here.
        eternal piste erection miuku gmail piste com


        Comment

        • Ken Robinson

          #5
          Re: Delete multiple spaces and special characters


          Mindy Geac wrote:[color=blue]
          > Is it possible to delete special caracters from a string and multiple
          > spaces?
          >
          > When the input is : "a&*bbb cc/c d!d"
          > I want the result : "abbb ccc dd"
          >
          > thnx.[/color]

          My feeling is KIS (Keep It Simple).

          Try this:

          <?php
          $str = 'a&*bbb cc/c d!d';
          $special = array('/','!','&','*');
          $str = str_replace(' ',' ',str_replace($ special,'',$str ));
          //
          // first remove all the special characters
          // then replace all consecutive two spaces with one space
          //
          echo '['.$str.']';
          ?>

          Ken

          Comment

          • kerul4u

            #6
            Re: Delete multiple spaces and special characters

            Yes Ken u r right
            Kimmo have u try putting two consecutive white space in

            $special = array('/','!','&','*'); //here you can add as many char. you
            want

            I think you have not read it carefully //here you can add as many char.
            you want

            KERUL
            [ProDesignZ]

            Comment

            • Mindy Geac

              #7
              Re: Delete multiple spaces and special characters

              So Im further right now, I need to put the hole ASCII table in de $special
              except :
              A..Z (0x41 0x5A
              a..z (0x61..0x7A
              0..9 (0x30..0x39)
              space (0x20)
              À..? (0xC0..0x259)?! ?

              Is there a better way to do this?

              -----------------------------
              "kerul4u" <....> wrote in message
              news:1125389507 .930327.137790@ z14g2000cwz.goo glegroups.com.. .[color=blue]
              > Yes Ken u r right
              > Kimmo have u try putting two consecutive white space in
              >
              > $special = array('/','!','&','*'); //here you can add as many char. you
              > want
              >
              > I think you have not read it carefully //here you can add as many char.
              > you want
              >
              > KERUL
              > [ProDesignZ]
              >[/color]


              Comment

              • juglesh

                #8
                Re: Delete multiple spaces and special characters


                Mindy Geac wrote:[color=blue]
                > So Im further right now, I need to put the hole ASCII table in de $special
                > except :
                > A..Z (0x41 0x5A
                > a..z (0x61..0x7A
                > 0..9 (0x30..0x39)
                > space (0x20)
                > À..? (0xC0..0x259)?! ?
                >
                > Is there a better way to do this?
                >
                > -----------------------------
                > "kerul4u" <....> wrote in message
                > news:1125389507 .930327.137790@ z14g2000cwz.goo glegroups.com.. .[color=green]
                > > Yes Ken u r right
                > > Kimmo have u try putting two consecutive white space in
                > >
                > > $special = array('/','!','&','*'); //here you can add as many char. you
                > > want
                > >
                > > I think you have not read it carefully //here you can add as many char.
                > > you want[/color][/color]

                I use this for foldernames:
                Not sure about alnum in the third one, if it allows foreign (to us in
                the states) characters.

                // clean up the user specified foldername
                $foldername = stripslashes ( $_POST['foldername'] );
                //This erase white-spaces on the beginning and the end in each line of
                a string:
                $foldername = preg_replace('~ ^(\s*)(.*?)(\s* )$~m', "\\2", $foldername);
                //erases all NON-alfanumerics
                $foldername = ereg_replace("[^[:alnum:] ]","",$foldernam e);
                // take out repetative spaces:
                $foldername = preg_replace('/\s\s+/', ' ', $foldername);
                if ($foldername == ""){$folder name = "untitled"; }

                Comment

                Working...