Created
June 16, 2014 16:55
-
-
Save chrisveness/7c34a3f18938f33d513c to your computer and use it in GitHub Desktop.
Convert string to SEO-friendly form (lowercase hyphenated alphanumeric words)
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 | |
/** | |
* Converts string to SEO-friendly form (lowercase hyphenated alphanumeric words) | |
* | |
* @param $string | |
* @return string | |
*/ | |
function seoUrl($string) | |
{ | |
// qv stackoverflow.com/questions/11330480, stackoverflow.com/questions/1017599 | |
$src = 'àáâãäçèéêëìíîïñòóôõöøùúûüýÿßÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝ'; | |
$rep = 'aaaaaceeeeiiiinoooooouuuuyysAAAAACEEEEIIIINOOOOOOUUUUY'; | |
// strip off accents (assuming utf8 PHP - note strtr() requires single-byte) | |
$string = strtr(utf8_decode($string), utf8_decode($src), $rep); | |
// convert to lower case | |
$string = strtolower($string); | |
// strip all but alphanumeric, whitespace, dot, underscore, hyphen | |
$string = preg_replace("/[^a-z0-9\s._-]/", "", $string); | |
// merge multiple consecutive whitespaces, dots, underscores, hyphens | |
$string = preg_replace("/[\s._-]+/", " ", $string); | |
// convert whitespaces to hyphens | |
$string = preg_replace("/[\s]/", "-", $string); | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment