Remove any character not a-Z

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Remove any character not a-Z

Post by Benjamin »

I'm trying to think of where to start on this. I need a way to remove all characters from a string that aren't letters. (and a few other characters that I can add/remove occassionally) The string will be fairly large, upwards of 300k in some cases, so obviously I don't want to parse it character by character. Any ideas?

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$newstring = preg_replace('#[a-zA-Z]+#', '', $oldstring);
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

feyd, Wouldn't that REMOVE the letters from the string... I was thinking more like:

Code: Select all

$newstring = preg_replace('#[^a-zA-Z]#', '', $oldstring);
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Yep, Robert Plank has the correct solution. The first one will remove A-Za-z
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I could have swore he said remove the letters....... hmm..

<- senile old man
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

feyd wrote:I could have swore he said remove the letters....... hmm..

<- senile old man
Hehe, yeah, i had to read it at least 7 times before my brain clicked
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Thanks guys I'll muck with the regex and try to add spaces and such. (I don't want all the spaces taken out). If I can't figure it out I'll ask.
Post Reply