Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Delete multiple spaces and special characters

 
   Database Forums (Home) -> PHP RSS
Next:  Software Engineers required for USA  
Author Message
Mindy Geac

External


Since: Aug 29, 2005
Posts: 2



(Msg. 1) Posted: Mon Aug 29, 2005 7:55 am
Post subject: Delete multiple spaces and special characters
Archived from groups: alt>comp>lang>php, others (more info?)

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

?>

 >> Stay informed about: Delete multiple spaces and special characters 
Back to top
Login to vote
John Dunlop1

External


Since: May 20, 2004
Posts: 28



(Msg. 2) Posted: Mon Aug 29, 2005 7:55 am
Post subject: Re: Delete multiple spaces and special characters [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

 >> Stay informed about: Delete multiple spaces and special characters 
Back to top
Login to vote
kerul4u

External


Since: Mar 21, 2005
Posts: 3



(Msg. 3) Posted: Mon Aug 29, 2005 7:55 am
Post subject: Re: Delete multiple spaces and special characters [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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]
 >> Stay informed about: Delete multiple spaces and special characters 
Back to top
Login to vote
Ken Robinson1

External


Since: Sep 28, 2004
Posts: 13



(Msg. 4) Posted: Mon Aug 29, 2005 7:55 am
Post subject: Re: Delete multiple spaces and special characters [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: Delete multiple spaces and special characters 
Back to top
Login to vote
Kimmo Laine

External


Since: Mar 21, 2005
Posts: 6



(Msg. 5) Posted: Mon Aug 29, 2005 9:55 am
Post subject: Re: Delete multiple spaces and special characters [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"kerul4u" wrote in message

> 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 Wink

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com
 >> Stay informed about: Delete multiple spaces and special characters 
Back to top
Login to vote
kerul4u

External


Since: Mar 21, 2005
Posts: 3



(Msg. 6) Posted: Tue Aug 30, 2005 1:11 am
Post subject: Re: Delete multiple spaces and special characters [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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]
 >> Stay informed about: Delete multiple spaces and special characters 
Back to top
Login to vote
Mindy Geac

External


Since: Aug 29, 2005
Posts: 2



(Msg. 7) Posted: Tue Aug 30, 2005 6:55 am
Post subject: Re: Delete multiple spaces and special characters [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

> 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]
>
 >> Stay informed about: Delete multiple spaces and special characters 
Back to top
Login to vote
juglesh

External


Since: May 06, 2005
Posts: 7



(Msg. 8) Posted: Tue Aug 30, 2005 11:53 am
Post subject: Re: Delete multiple spaces and special characters [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
>
> > 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";}
 >> Stay informed about: Delete multiple spaces and special characters 
Back to top
Login to vote
Display posts from previous:   
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]