Quick Tip: Search Engine Friendly URL From Title In PHP

Posted March 10, 2009 @ 10:10am by Phil

Here is a little function I wrote to convert my post titles into search engine friendly URLs.

<?php
  function title2url ($title)
  {
    // Convert to lowercase
    $title = strtolower($title);
    // Strip everything but letters, numbers, and whitespace
    $title = preg_replace("/[^a-z0-9\s]/", "", $title);
    // Trim whitespace from beginning and end
    $title = trim($title);
    // Convert remaining whitespace to dashes
    $title = preg_replace("/[\s]+/", "-", $title);
    return $title;
  }
?>

Download Source: title2url.php

Tags: PHP/MySQL, Quick Tip, Webmasters Tips