How to convert a string to slug in PHP

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;
}

Please Share us:
Please rate this snippet

2 thoughts on “How to convert a string to slug in PHP

Leave a Reply