Last active
November 21, 2017 18:02
-
-
Save hh-com/3a594605a283fb4a433454bfe50315ab to your computer and use it in GitHub Desktop.
PHP Function to generate an Alias for URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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('[&]', '[&]', '[lt]', '[gt]', '[nbsp]', '[-]'), array('&', '&', '<', '>', ' ', '­'), $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