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

regexp to leave only alpha/numeric chars

Using preg_replace() is there a simple regexp to strip everything from a
string except alpha and numeric chars (a-zA-Z0-9)?

$input = "$tring1!";
$pattern =
$input = preg_replace($pattern, "", $input);

result: "tring1"

Jul 17 '05 #1
5 9013
Bosconian wrote:
Using preg_replace() is there a simple regexp to strip everything from a
string except alpha and numeric chars (a-zA-Z0-9)?

$input = "$tring1!";
$pattern =
$input = preg_replace($pattern, "", $input);

result: "tring1"


Chances are anything you will ever try to do with regexp's has been done,
and there may even be a shortcut for it. So at this page:

http://www.php.net/manual/en/referen...ern.syntax.php

you would be able to find this:

$input = preg_replace('\W','',$input)

This will leave in underscores, but that is easily fixed:

$input = preg_replace('[\W_]','',$input)

Also try reading an introduction to regexp's in a Perl guide, you can likely
find something more of a tutorial there to get you started.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #2
"Kenneth Downs" <kn**************@see.sigblock> wrote in message
news:3i************@pluto.downsfam.net...
Bosconian wrote:
Using preg_replace() is there a simple regexp to strip everything from a
string except alpha and numeric chars (a-zA-Z0-9)?

$input = "$tring1!";
$pattern =
$input = preg_replace($pattern, "", $input);

result: "tring1"
Chances are anything you will ever try to do with regexp's has been done,
and there may even be a shortcut for it. So at this page:

http://www.php.net/manual/en/referen...ern.syntax.php

you would be able to find this:

$input = preg_replace('\W','',$input)

This will leave in underscores, but that is easily fixed:

$input = preg_replace('[\W_]','',$input)

Also try reading an introduction to regexp's in a Perl guide, you can

likely find something more of a tutorial there to get you started.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)


Ken,

I was able to use your example, but not without adding starting and ending
delimiters:

'/[\W_]/'

Nothing was stripped without them.

I will bone-up on my regexp syntax.

Many thanks.
Jul 17 '05 #3
Bosconian wrote:
I was able to use your example, but not without adding starting and ending
delimiters: '/[\W_]/'


You might find '/[\W_]+/' faster. Also, on the linked doc page it says
the \w and \W are locale specific; there might be accented characters
left behind. If you don't want that, use '/[^a-z0-9]+/i'.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4
Bosconian wrote:
"Kenneth Downs" <kn**************@see.sigblock> wrote in message
news:3i************@pluto.downsfam.net...
Bosconian wrote:
> Using preg_replace() is there a simple regexp to strip everything from
> a string except alpha and numeric chars (a-zA-Z0-9)?
>
> $input = "$tring1!";
> $pattern =
> $input = preg_replace($pattern, "", $input);
>
> result: "tring1"


Chances are anything you will ever try to do with regexp's has been done,
and there may even be a shortcut for it. So at this page:

http://www.php.net/manual/en/referen...ern.syntax.php

you would be able to find this:

$input = preg_replace('\W','',$input)

This will leave in underscores, but that is easily fixed:

$input = preg_replace('[\W_]','',$input)

Also try reading an introduction to regexp's in a Perl guide, you can

likely
find something more of a tutorial there to get you started.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)


Ken,

I was able to use your example, but not without adding starting and ending
delimiters:

'/[\W_]/'

Nothing was stripped without them.

I will bone-up on my regexp syntax.

Many thanks.


I would love to say I left the delimiters out on purpose to improve your
skills, but it would be a lie. I just forgot.

Glad it helped.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #5
"Ewoud Dronkert" <fi*******@lastname.net.invalid> wrote in message
news:42*********************@dreader4.news.xs4all. nl...
Bosconian wrote:
I was able to use your example, but not without adding starting and ending delimiters: '/[\W_]/'


You might find '/[\W_]+/' faster. Also, on the linked doc page it says
the \w and \W are locale specific; there might be accented characters
left behind. If you don't want that, use '/[^a-z0-9]+/i'.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/


Even better. Thanks for the tip.
Jul 17 '05 #6

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

Similar topics

2
by: entoone | last post by:
I have a field called pword, whenever someone enters anything but numeric, i.e. mixed alpha with numeric, or even just alpha.. the following error appears. Warning: mysql_numrows(): supplied...
0
by: John K. Humkey | last post by:
Ok, it's sed not perl, but it's primarily a regexp question that I've encountered (and failed to understand) before. . . # # When trying to convert a variable number # of spaces separating...
0
by: Ed Leafe | last post by:
I recently upgraded to 4.1 alpha (MySQL 4.1.0-alpha-standard-log) on my Linux server, and came across a problem with a query that had been working in 3.23 that no longer worked in 4.1a. I've...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: success_ny | last post by:
Does anyone have a code snippet to compare those values so I can sort the array of alpha-numeric values that include both characters and integers in it? I.e., if we have values like 4236 and...
6
by: Mark C | last post by:
All, Is there such a function that can strip all non alpha ( not between a-z) characters from a string? I have a function that I currently use that will strip one character at a time from a...
8
by: MLH | last post by:
I have a textbox on a form into which an alpha-numeric string of data is entered. I wish to force the casual user, who would sometimes use upper case, sometimes not and sometimes MIX the case -...
6
by: Edward | last post by:
I need to validate a text box entry, but ONLY if it is 17 characters, otherwise I have to ignore it. My regular expression for the validation is: ^(({9})()()(\d{6}))$ Can I adapt this to...
5
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...
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...

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.