Last active
April 6, 2016 16:10
-
-
Save brunomonteiro3/6682a908ef0cf05a831d7c2d587f3555 to your computer and use it in GitHub Desktop.
SEO friendly URL. "Löng Strîng" will be converted to > "long-string".
This file contains hidden or 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 | |
function seoUrl($string, $separator = '-') { | |
$accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i'; | |
$special_cases = array( '&' => 'and', "'" => ''); | |
$string = mb_strtolower( trim( $string ), 'UTF-8' ); | |
$string = str_replace( array_keys($special_cases), array_values( $special_cases), $string ); | |
$string = preg_replace( $accents_regex, '$1', htmlentities( $string, ENT_QUOTES, 'UTF-8' ) ); | |
$string = preg_replace("/[^a-z0-9]/u", "$separator", $string); | |
$string = preg_replace("/[$separator]+/u", "$separator", $string); | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment