Professional Web Applications Themes

Delete multiple spaces and special characters - PHP Development

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 ?>...

  1. #1

    Default 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

    ?>







    Mindy Guest

  2. #2

    Default Re: Delete multiple spaces and special characters

    Mindy Geac wrote:
     

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

    --
    Jock
    John Guest

  3. #3

    Default 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($special,'',$string);

    KERUL
    [ProDesignZ]

    kerul4u Guest

  4. #4

    Default Re: Delete multiple spaces and special characters

    "kerul4u" <com> wrote in message
    news:googlegroups.com... 


    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


    Kimmo Guest

  5. #5

    Default Re: Delete multiple spaces and special characters


    Mindy Geac wrote: 

    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

    Ken Guest

  6. #6

    Default 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]

    kerul4u Guest

  7. #7

    Default 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:googlegroups.com... 


    Mindy Guest

  8. #8

    Default Re: Delete multiple spaces and special characters


    Mindy Geac wrote: [/ref]

    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:] ]","",$foldername);
    // take out repetative spaces:
    $foldername = preg_replace('/\s\s+/', ' ', $foldername);
    if ($foldername == ""){$foldername = "untitled";}

    juglesh Guest

Similar Threads

  1. Special Character And Extra Spaces
    By WEBDavid in forum Macromedia Contribute General Discussion
    Replies: 3
    Last Post: March 27th, 06:53 AM
  2. Delete spaces from variabele
    By InSeCo in forum PHP Development
    Replies: 2
    Last Post: April 14th, 07:43 AM
  3. More special characters in FH
    By maciekk in forum Macromedia Freehand
    Replies: 1
    Last Post: July 19th, 11:11 AM
  4. Special characters #,$ etc
    By Mark Denham in forum Informix
    Replies: 1
    Last Post: August 1st, 05:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139