Title “keywordize” function

This function is used to fetch a valid keyword string for use in a URL. This function comes in handy when you want to structure your URLs to contain keywords found in the page’s title, for example.

Any non alphanumeric characters are stripped from the string, and spaces are replaced with dashes. Extra whitespace is accounted for.

Example:

$title = 'This is the "Best" Function Ever!';
echo keywordize($title);

Output:

this-is-the-best-function-ever

Code:

  function keywordize($string){
    return preg_replace(array('/^[^a-z0-9]|[^a-z0-9]+$/', '/ +|(\.{2,}[ ]?)+/s', '/[^a-z0-9_-]/s', '/(-{2,})/', '/((^-)|(-$))/'), array('', '-', '', '-', ''), strtolower($string));
  }

You can use the output of this function in your URLs like most CMS applications do to make your URLs search engine friendly.

You can add weight to the keywords within you page by containing them within the page’s URL.

I’ve written a short tutorial on adding search engine friendly keywords to a URL with ModRewrite.

One Response to “Title “keywordize” function”

  1. [...] I have posted a PHP function that will keywordize a page title or description. [...]

Leave a Reply