For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > August 2005 > Delete multiple spaces and special characters









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Delete multiple spaces and special characters
Mindy Geac

2005-08-29, 7:55 am

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

2005-08-29, 7:55 am

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
kerul4u

2005-08-29, 7:55 am

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]

Kimmo Laine

2005-08-29, 7:55 am

"kerul4u" <kerul4u@gmail.com> wrote in message
news:1125318171.067211.108700@z14g2000cwz.googlegroups.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


Ken Robinson

2005-08-29, 6:56 pm


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

kerul4u

2005-08-30, 3:55 am

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]

Mindy Geac

2005-08-30, 6:56 pm

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.googlegroups.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]
>



juglesh

2005-08-30, 6:56 pm


Mindy Geac wrote:[color=darkred]
> 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)
> =C0..? (0xC0..0x259)?!?
>
> Is there a better way to do this?
>
> -----------------------------
> "kerul4u" <....> wrote in message
> news:1125389507.930327.137790@z14g2000cwz.googlegroups.com...
ou[color=darkred]

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 =3D stripslashes ( $_POST['foldername'] );
//This erase white-spaces on the beginning and the end in each line of
a string:
$foldername =3D preg_replace('~^(\s*)(.*?)(\s*)$~m', "\\2", $foldername);
//erases all NON-alfanumerics
$foldername =3D ereg_replace("[^[:alnum:] ]","",$foldername);
// take out repetative spaces:
$foldername =3D preg_replace('/\s\s+/', ' ', $foldername);
if ($foldername =3D=3D ""){$foldername =3D "untitled";}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com