By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

Delete multiple spaces and special characters

P: n/a
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

?>



Aug 29 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Mindy Geac wrote:
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"


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

--
Jock
Aug 29 '05 #2

P: n/a
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]

Aug 29 '05 #3

P: n/a
"kerul4u" <ke*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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);

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
Aug 29 '05 #4

P: n/a

Mindy Geac wrote:
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.


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

Aug 29 '05 #5

P: n/a
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]

Aug 30 '05 #6

P: n/a
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:11**********************@z14g2000cwz.googlegr oups.com...
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]

Aug 30 '05 #7

P: n/a

Mindy Geac wrote:
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:11**********************@z14g2000cwz.googlegr oups.com...
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


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";}

Aug 30 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.