Skip to content

Instantly share code, notes, and snippets.

@hh-com
Last active November 21, 2017 18:02
Show Gist options
  • Save hh-com/3a594605a283fb4a433454bfe50315ab to your computer and use it in GitHub Desktop.
Save hh-com/3a594605a283fb4a433454bfe50315ab to your computer and use it in GitHub Desktop.
PHP Function to generate an Alias for URL
<?php
public function generateAlias($strString, $uniquer)
{
$strString = preg_replace('/(&#*\w+)[\x00-\x20]+;/i', '$1;', $strString);
$strString = preg_replace('/(&#x*)([0-9a-f]+);/i', '$1$2;', $strString);
$strString = html_entity_decode($strString, ENT_COMPAT, null);
$strString = str_replace(array('[&]', '[&amp;]', '[lt]', '[gt]', '[nbsp]', '[-]'), array('&amp;', '&amp;', '&lt;', '&gt;', '&nbsp;', '&shy;'), $strString);
$arrSearch = array('/[^a-zA-Z0-9 \.\&\/_-]+/', '/[ \.\&\/-]+/');
$arrReplace = array('', '-');
$strString = preg_replace($arrSearch, $arrReplace, $strString);
if (is_numeric(substr($strString, 0, 1)))
{
$strString = 'id-' . $strString;
}
$strString = trim($strString, '-');
return $strString . "-" . ($uniquer ? $uniquer : rand(100000,999999) );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment