Jump to content

[SOLVED] I'm thinking preg_replace might get this done?


immanuelx2

Recommended Posts

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.

$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.

$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!! :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.