title: Generate SEO friendly URLs (slugs)
body: <h1>Definition</h1>
  <p>From <a href="http://en.wikipedia.org/wiki/Slug_%28web_publishing%29" rel="noreferrer">Wikipedia</a>:</p>
  <blockquote>
  <p>A <strong>slug</strong> is the part of a URL which identifies a page using
  human-readable keywords.</p>
  <p>To make the URL easier for users to type, special characters are often
  removed or replaced as well. For instance, accented characters are
  usually replaced by letters from the English alphabet; punctuation
  marks are generally removed; and spaces (which have to be encoded as
  %20 or +) are replaced by dashes (-) or underscores (_), which are
  more aesthetically pleasing.</p>
  </blockquote>
  <h1>Context</h1>
  <p>I developed a photo-sharing website on which users can upload, share and view photos.</p>
  <p>All pages are generated automatically without my grip on the title. Because the title of a photo or the name of a user may contain accented characters or spaces, I needed a function to automatically create slugs and keep readable URLs.</p>
  <p>I created the following function which replaces accented characters (âèêëçî), removes punctuation and bad characters (#@&amp;~^!) and transforms spaces in dashes.</p>
  <h1>Questions</h1>
  <ul>
  <li>What do you think about this function?</li>
  <li>Do you know any other functions to create slugs?</li>
  </ul>
  <h1>Code</h1>
  <p><strong><a href="/questions/tagged/php" class="post-tag" title="show questions tagged &#39;php&#39;" rel="tag">php</a>:</strong></p>
  <pre><code>function sluggable($str) {
  
      $before = array(
          'àáâãäåòóôõöøèéêëðçìíîïùúûüñšž',
          '/[^a-z0-9\s]/',
          array('/\s/', '/--+/', '/---+/')
      );
   
      $after = array(
          'aaaaaaooooooeeeeeciiiiuuuunsz',
          '',
          '-'
      );
  
      $str = strtolower($str);
      $str = strtr($str, $before[0], $after[0]);
      $str = preg_replace($before[1], $after[1], $str);
      $str = trim($str);
      $str = preg_replace($before[2], $after[2], $str);
   
      return $str;
  }
  </code></pre>