แปลง string slug ให้เป็นรูปแบบ URL-friendly

URL-friendly

แปลตรงๆ url friendly ก็คือที่อยู่ของเว็บไซต์ที่ดูเป็นมิตร ( กับผู้ใช้ ) เป็นประโยชน์ในการสื่อความหมายและก่อให้เกิดความเข้าใจ ทั้งต่อผู้ใช้งานและบรรดา search engine

<?php
function slug_url($text) {
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^-\w]+~', '', $text);
$text = preg_replace('~-+~', '-', $text);
$text = strtolower($text);
$text = trim($text, " \t\n\r\0\x0B-");
if (empty($text)) {
return 'n-a';
}
return $text;
}

print(slug_url('PHP Tutorial'));
?>

Output: php-tutorial