473,597 Members | 2,341 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($p attern, "", $input);

result: "tring1"

Jul 17 '05 #1
5 9028
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($p attern, "", $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.down sfam.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($p attern, "", $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.down sfam.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($p attern, "", $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*******@last name.net.invali d> wrote in message
news:42******** *************@d reader4.news.xs 4all.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
2386
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 argument is not a valid MySQL result resource What do I need to change the field to, to be able to have whatever password configuration the user wants to enter?
0
2978
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 fields, to fields # separated by single commas, if your "input" # file looks like the following lines. . . # #alpha bravo charlie delta echo fox
0
1382
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 simplified the query to demonstrate the problem: select trim(upper('foo')) regexp ('^foo$') as trimUpper, upper('foo') regexp ('^foo$') as justUpper, trim('foo') regexp ('^foo$') as justTrim, trim(upper('foo')) as trimUpperFoo, upper('foo') as...
7
3253
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)) CType(local4, Short) = CType(src, Short)
3
6516
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 123234, I want 4236 to be second because 4 is bigger than 1 rather than using the numeric comparison. The strings can include character values and strings. Basically, I have the bubble sort function, the question is how to compare those types of...
6
9082
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 string that I tried tweaking without success. Any help would be much appreciated. Thanks in advance, Mark C. hereinde@yahoo.com
8
31801
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 - yes, believe it - to use UPPER case only. I tried running UCASE on the string entered during the textbox's BeforeUpdate event code. That did nothing but return some error saying I couldn't do it there - some kind-a-race condition, I dunno - can't...
6
1829
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 "fire" only if the string in question is 17 chars in length? Or do I have to do this server-side? Thanks
5
28556
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 AlphaNumeric = 2 Numeric = 3 End Enum
0
7971
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8040
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6698
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2408
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
0
1243
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.