473,570 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

?>



Aug 29 '05 #1
7 35896
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
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]

Aug 29 '05 #3
"kerul4u" <ke*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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($sp ecial,'',$strin g);

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

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
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
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.goo glegroups.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

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

Aug 30 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2537
by: panorris | last post by:
I am an infrequent user of Access and have a problem which I hope someone may be able to help me with. I have a text field in a database which contains UK postcodes similar to EH4 7TY or DN10 9IU. As shown in the two examples, a postcode can either have seven characters including the space in the middle or 8 characters including the space...
32
14785
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
4
3119
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to replace multiple entries of an object within a string. That is, if a user hit twice to create one new line, but just once to create another, I want...
22
4165
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. How can I delete the node from DOM in other way? Thanks.
2
6537
by: David | last post by:
Sent this to alt.php a couple of days back, but doesn't look like I'll get an answer, so trying here. I'm trying to convert a script to use friendly URLs, I've done this before, but my PHP skills are quite basic so far, far from proficient at this. ..htaccess file- DirectoryIndex default.php index.asp index.html index.htm index.php
7
11201
by: ClarkePeters | last post by:
I have large text files that I read into an array, but before that I take out all the special characters such as tabs, new lines, and returns. However I'm left with all the extra spaces (sometimes as many as 20 or more at a time) because my users are allowed to type spaces in their text. For storage and other manipulation, I don't need all those...
4
2203
by: - HAL9000 | last post by:
When un-installing an application... Is it normal practice to write a special program that erases all the files and folders for all the users of an application that reads and writes to SpecialFolder.ApplicationData (C:\Documents and Settings\Username\Application Data) ? Or, is there some way to get the ms installer to do this for you? ...
4
1821
by: gentsquash | last post by:
On some of my course pages, I quote (with attribution) small sections of Wikipedia and the like. E.g, the top of http://en.wiktionary.org/wiki/entropy has "entropia" in Greek font, http://en.wikipedia.org/wiki/Goedel has the o-umlaut from German, and
3
4794
by: bstjean | last post by:
Hi everyone, I am trying to find an efficient way to perform a special query. Let me explain what I want. Let's say we are looking for all description that match "this is the target". In fact, I want to find records that match those 4 words in this sequence disregarding the number of spaces (I mean spaces, tabs, Cr, Lf, etc) between...
0
7726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7636
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7948
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7700
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5523
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.