Non-Regex function to remove non-alphanumeric characters?

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
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Non-Regex function to remove non-alphanumeric characters?

Post by JAB Creations »

Is there a non-regex function to remove non-alphanumeric characters from a string? I'm trying to convert blog tags to be SEO/URL friendly so in example "application/xhtml+xml" is still SEO friendly as "applicationxhtmlxml".

I could do this though I'm wondering if there are any other options?

Code: Select all

$new_string = preg_replace("/[^a-zA-Z0-9]/", "", $string);
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Non-Regex function to remove non-alphanumeric characters?

Post by Jade »

Is there a reason why you don't want to use regex?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Non-Regex function to remove non-alphanumeric characters?

Post by JAB Creations »

Load.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Non-Regex function to remove non-alphanumeric characters?

Post by Jade »

I'm not sure you have much alternative. Nothing I can think of anyways. My best suggestion would be to store them after they've been converted instead of replacing every time.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Non-Regex function to remove non-alphanumeric characters?

Post by JAB Creations »

True, I'd rather store an extra column instead of endlessly executing regex over and over. Unless any there are any alternative suggestions that is the route I'll go with for now.
Post Reply