Skip to content

Instantly share code, notes, and snippets.

@peterdemartini
Created December 11, 2013 00:37
Show Gist options
  • Save peterdemartini/7903132 to your computer and use it in GitHub Desktop.
Save peterdemartini/7903132 to your computer and use it in GitHub Desktop.
Search Engine Friendly String converter.
<?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