To convert a string to slug in PHP you can use the following snippet.

Sample PHP

function StringToSlug($input, $replacement){
	(!isset($replacement) || trim($replacement)===''){
		$replacement = "/"
	}
	$input = strtolower(trim($input));
	$input = preg_replace('/[^a-z0-9-]/', $replacement, $input);
	$input = preg_replace('/-+/', $replacement, $input);
	return $input;
}

2 thought on “How to convert a string to slug in PHP”

Leave a Reply