Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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