Created
December 11, 2013 00:37
-
-
Save peterdemartini/7903132 to your computer and use it in GitHub Desktop.
Search Engine Friendly String converter.
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 generate_seo_link($string, $replace = '-'){ | |
$string = strtolower($string); | |
//remove query string | |
if(preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i",$string)){ | |
$parsed_url = parse_url($string); | |
$string = $parsed_url['host'].' '.$parsed_url['path']; | |
} | |
//replace / and . with white space | |
$string = preg_replace("/[\/\.]/", " ", $string); | |
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string); | |
//remove multiple dashes or whitespaces | |
$string = preg_replace("/[\s-]+/", " ", $string); | |
//convert whitespaces and underscore to $replace | |
$string = preg_replace("/[\s_]/", $replace, $string); | |
//limit the slug size | |
$string = substr($string, 0, 100); | |
//seo link is generated | |
return $string; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment