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! 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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'...
|
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...
|
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...
|
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('/\\\"/', '"', $tekst);
In my forms, I would like to change " to " but this line is actualy...
|
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
...
|
by: Andy |
last post by:
Does anyone know of a deduplication and data cleansing tool that works with
Access databases?
Thanks
Andy
|
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$", "",...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |