473,386 Members | 1,827 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,386 software developers and data experts.

Cleansing with preg_replace


Folks,

I have a string that *could* contain any character - I want it to call a
function that will allow me to pass the variable, and then return it cleaned
of anything except alphanumeric, dots, dashes and commas... How would I do
this? I have a feeling preg_replace is what I need to use but regular
expressions have always been over my head. I do know the syntax though
(taken from a previous posting made some weeks ago) that I've been trying to
play with - the previous post had

check to see if a forename field entry is composed only of letters
if(!preg_match("/^[a-zA-Z]+$/",$_POST["Nforename"]))
{
// invalid form entry. go back to form
}

with /^[0-9]+$/ - only digits 0 - 9 allowed

with /^[a-zA-Z\s]+$/ - letters and spaces

with /^[a-zA-Z0-9\.]$/ - letters, numbers and periods
I've been trying something like
$myString="abc'de!789()";
$myCleanedString=preg_replace("/^[a-zA-Z0-9\.]$/", "/^[a-zA-Z0-9\.]$/",
$myString);

(I thought it would search characters listed in the frist arguement and
replace them with characters in the second arguement and ignore everything
else - but I was wrong).

Can someone help me here?

Thanks!
Jul 16 '05 #1
1 5766

"CC Zona" <cc****@nospam.invalid> wrote in message
news:cc**************************@netnews.attbi.co m...
In article <kY***********************@news3.calgary.shaw.ca >,
"Randell D." <yo**************************@yahoo.com> wrote:
I have a string that *could* contain any character - I want it to call a
function that will allow me to pass the variable, and then return it cleaned of anything except alphanumeric, dots, dashes and commas...
$cleaned= preg_replace("/[^a-z\d.,-]/i",'',$string);

(If you want to keep all whitespace characters too, add "\s"--no

quotes--to the class)

--
CC


Thanx
Jul 16 '05 #2

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

Similar topics

3
by: JDJones | last post by:
I'm having some trouble getting this replacement to work efficiently. I have the following code: $thing = preg_replace("(\r\n|\n|\r|\t)", "", $thing1); What I want to do now is to get it to...
3
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str'...
1
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the...
7
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore...
2
by: Fotozine | last post by:
well, I have this peace of code which isn't woking as I would like it to work. $tekst = preg_replace('/\\\"/', '&quot;', $tekst); In my forms, I would like to change " to &quot; but this line is actualy...
3
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
5
by: Andy | last post by:
Does anyone know of a deduplication and data cleansing tool that works with Access databases? Thanks Andy
5
by: julianmlp | last post by:
I'm fairly new to regex code, and I'm trying to find a way to replace any text after "///" (three slashes) and before a "\n" character. I've tried with: $filen = preg_replace("///./\n$", "",...
8
by: erikcw | last post by:
Hi all, I'm trying to write a regex pattern to use in preg_replace. Basically I want to put around every line (\n) in this variable. However, I need to exclude lines that already have brackets...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...

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.