Page 1 of 1

Remove any character not a-Z

Posted: Fri Jun 23, 2006 12:07 am
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!

Posted: Fri Jun 23, 2006 12:16 am
by feyd

Code: Select all

$newstring = preg_replace('#[a-zA-Z]+#', '', $oldstring);

Posted: Fri Jun 23, 2006 9:56 am
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);

Posted: Fri Jun 23, 2006 10:19 am
by JayBird
Yep, Robert Plank has the correct solution. The first one will remove A-Za-z

Posted: Fri Jun 23, 2006 10:23 am
by feyd
I could have swore he said remove the letters....... hmm..

<- senile old man

Posted: Fri Jun 23, 2006 10:25 am
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

Posted: Fri Jun 23, 2006 12:07 pm
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.