generate seo friendly url from string in php
<?php
/**
* Generate a URL friendly "slug" from a given string.
*
* @param string $title
* @param string $separator
* @return string
*/
function slug($title, $separator = '-')
{
// convert String to Utf-8 Ascii
$title = iconv(mb_detect_encoding($title, mb_detect_order(), true), "UTF-8", $title);
// Convert all dashes/underscores into separator
$flip = $separator == '-' ? '_' : '-';
$title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
// Remove all characters that are not the separator, letters, numbers, or whitespace.
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
return trim($title, $separator);
}
$string = "She (sells) seA shells at -the SEA shore?";
$slug = slug($string);
echo $slug;
?>
//Output is: she-sells-sea-shells-at-the-sea-shore