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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.