immanuelx2 Posted July 19, 2009 Share Posted July 19, 2009 Hi all. I am looking to generate SEO-url-friendly titles. I'm guessing some sort of preg_replace function will be able to get the job done. Unfortunately I'm still a beginner with regular expressions.\ I'm looking to do something like the following: from: This Is a Great Article Title; With 'Quotes,' Numb3rs, commas, and Semicolons to: this-is-a-great-article-title-with-quotes-numbers-commas-and-semicolons Basically strip out EVERYTHING except for numbers and letters, and replace the spaces with dashes. Is this possible? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
.josh Posted July 19, 2009 Share Posted July 19, 2009 $string = str_replace(' ','-',$string); // replace all spaces with hyphen $string = preg_replace('~[^-a-z0-9]+~i','',$string); // remove anything not alphanumeric or hyphen with $string = strtolower($string); // make it lowercase? (that's what you have in your from -> to example As far as changing for instance "Numb3rs" to "numbers" (changing the 3 to an e) that's a bit more complex. Think it would be practically impossible to transform arbitrary text like that. I suppose you can catch most of those by first exploding at a space (or dash, if you have it post-str_replace), then loop through each element and check to see if it's a combo of words and letters (to avoid changing for instance 333 to eee), and if so, loop through each letter and check against an array containing "3" => 'e', "0" => 'o', etc... and replace if found. But that's not going to be perfect, as you will end up changing 3rd to erd, sort of thing. you could further have a list of exceptions, like number, followed by st,nd,rd,th I guess. Quote Link to comment Share on other sites More sharing options...
immanuelx2 Posted July 20, 2009 Author Share Posted July 20, 2009 $string = str_replace(' ','-',$string); // replace all spaces with hyphen $string = preg_replace('~[^-a-z0-9]+~i','',$string); // remove anything not alphanumeric or hyphen with $string = strtolower($string); // make it lowercase? (that's what you have in your from -> to example As far as changing for instance "Numb3rs" to "numbers" (changing the 3 to an e) that's a bit more complex. Think it would be practically impossible to transform arbitrary text like that. I suppose you can catch most of those by first exploding at a space (or dash, if you have it post-str_replace), then loop through each element and check to see if it's a combo of words and letters (to avoid changing for instance 333 to eee), and if so, loop through each letter and check against an array containing "3" => 'e', "0" => 'o', etc... and replace if found. But that's not going to be perfect, as you will end up changing 3rd to erd, sort of thing. you could further have a list of exceptions, like number, followed by st,nd,rd,th I guess. Thanks a lot for the example. Actually, that Numb3rs to numbers was an error on my part lol.. I just wanted to include a number to allow them in the output as well, but I forgot to put the 3 in the output!! Thanks again!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.