473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

KERUL
[ProDesignZ]

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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....
32
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...
4
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...
22
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. ...
2
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...
7
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...
4
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...
4
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, ...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.