By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,967 Members | 2,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,967 IT Pros & Developers. It's quick & easy.

Function to strip all but letters, numbers, and _?

P: n/a
Does anyone have a handy-dandy function they can quickly post that strips
everything from a string except for letters, numbers, & underscores?

--
[ Sugapablo ]
[ http://www.sugapablo.com <--music ]
[ http://www.sugapablo.net <--personal ]
[ su*******@12jabber.com <--jabber IM ]

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sat, 15 Jan 2005 14:59:18 -0500, Sugapablo <ru**@REMOVEsugapablo.com>
wrote:
Does anyone have a handy-dandy function they can quickly post that strips
everything from a string except for letters, numbers, & underscores?


$stripped = preg_replace('/[^a-z0-9_]/i','',$string);
--
* html {redirect-to: url(http://pornel.ldreams.net);}
Jul 17 '05 #2

P: n/a
*** Sugapablo escribió/wrote (Sat, 15 Jan 2005 14:59:18 -0500):
Does anyone have a handy-dandy function they can quickly post that strips
everything from a string except for letters, numbers, & underscores?


function strip_all_but_letters_numbers_and_underscores($tex t){
return preg_replace('/[^\w\d_]+/', '', $text);
}

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Jul 17 '05 #3

P: n/a
"Alvaro G. Vicario" <kA*****************@terra.es> wrote in message
news:1u****************************@40tude.net...
*** Sugapablo escribió/wrote (Sat, 15 Jan 2005 14:59:18 -0500):
Does anyone have a handy-dandy function they can quickly post that strips everything from a string except for letters, numbers, & underscores?
function strip_all_but_letters_numbers_and_underscores($tex t){
return preg_replace('/[^\w\d_]+/', '', $text);
}

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la

intemperie) ++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--


Eh, \w includes digits and the underscore. Also, capital letters are used to
denote "not a meta character." Hence the following:

preg_replace('/\W+/', '', $text);
Jul 17 '05 #4

P: n/a
*** Chung Leong escribió/wrote (Sat, 15 Jan 2005 23:07:33 -0500):
Eh, \w includes digits and the underscore. Also, capital letters are used to
denote "not a meta character." Hence the following:

preg_replace('/\W+/', '', $text);


Absolutely right. My code is what happens when you trust your memory :)

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.