Skip to content

Instantly share code, notes, and snippets.

@DaveChild
Created July 24, 2009 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveChild/154025 to your computer and use it in GitHub Desktop.
Save DaveChild/154025 to your computer and use it in GitHub Desktop.
Generate a URL portion or alias from any text - uses alphanumeric characters and hyphens only.
<?php
function generate_url_from_text($strText) {
$strText = preg_replace('/[^A-Za-z0-9-]/', ' ', $strText);
$strText = preg_replace('/ +/', ' ', $strText);
$strText = trim($strText);
$strText = str_replace(' ', '-', $strText);
$strText = preg_replace('/-+/', '-', $strText);
$strText = strtolower($strText);
return $strText;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment