Created
December 8, 2013 06:21
-
-
Save angelmartz/7853933 to your computer and use it in GitHub Desktop.
Slug SEO URL
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 | |
| static public function slugify($text) | |
| { | |
| // replace non letter or digits by - | |
| $text = preg_replace('~[^\\pL\d]+~u', '-', $text); | |
| // trim | |
| $text = trim($text, '-'); | |
| // transliterate | |
| $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); | |
| // lowercase | |
| $text = strtolower($text); | |
| // remove unwanted characters | |
| $text = preg_replace('~[^-\w]+~', '', $text); | |
| // -- Remove duplicated spaces | |
| $stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $string); | |
| if (empty($text)) | |
| { | |
| return 'n-a'; | |
| } | |
| return $text; | |
| } |
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 | |
| echo sanitize('testing this link'); | |
| //taken from wordpress | |
| function utf8_uri_encode( $utf8_string, $length = 0 ) { | |
| $unicode = ''; | |
| $values = array(); | |
| $num_octets = 1; | |
| $unicode_length = 0; | |
| $string_length = strlen( $utf8_string ); | |
| for ($i = 0; $i < $string_length; $i++ ) { | |
| $value = ord( $utf8_string[ $i ] ); | |
| if ( $value < 128 ) { | |
| if ( $length && ( $unicode_length >= $length ) ) | |
| break; | |
| $unicode .= chr($value); | |
| $unicode_length++; | |
| } else { | |
| if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3; | |
| $values[] = $value; | |
| if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length ) | |
| break; | |
| if ( count( $values ) == $num_octets ) { | |
| if ($num_octets == 3) { | |
| $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]); | |
| $unicode_length += 9; | |
| } else { | |
| $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]); | |
| $unicode_length += 6; | |
| } | |
| $values = array(); | |
| $num_octets = 1; | |
| } | |
| } | |
| } | |
| return $unicode; | |
| } | |
| //taken from wordpress | |
| function seems_utf8($str) { | |
| $length = strlen($str); | |
| for ($i=0; $i < $length; $i++) { | |
| $c = ord($str[$i]); | |
| if ($c < 0x80) $n = 0; # 0bbbbbbb | |
| elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb | |
| elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb | |
| elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb | |
| elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb | |
| elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b | |
| else return false; # Does not match any model | |
| for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ? | |
| if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80)) | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| //function sanitize_title_with_dashes taken from wordpress | |
| function sanitize($title) { | |
| $title = strip_tags($title); | |
| // Preserve escaped octets. | |
| $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title); | |
| // Remove percent signs that are not part of an octet. | |
| $title = str_replace('%', '', $title); | |
| // Restore octets. | |
| $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title); | |
| if (seems_utf8($title)) { | |
| if (function_exists('mb_strtolower')) { | |
| $title = mb_strtolower($title, 'UTF-8'); | |
| } | |
| $title = utf8_uri_encode($title, 200); | |
| } | |
| $title = strtolower($title); | |
| $title = preg_replace('/&.+?;/', '', $title); // kill entities | |
| $title = str_replace('.', '-', $title); | |
| $title = preg_replace('/[^%a-z0-9 _-]/', '', $title); | |
| $title = preg_replace('/\s+/', '-', $title); | |
| $title = preg_replace('|-+|', '-', $title); | |
| $title = trim($title, '-'); | |
| return $title; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment