Remove trailing whitespace from Title saved as Slug for SEO Friendly URLs
I am trying to create a slug for SEO friendly URLs from a title using
<?php echo (preg_replace('/[^A-Za-z0-9-]+/', '-', str_replace( "'", '', preg_replace( '/[«»""!?,.!@£$%^&*{};:()]+/', '', strtolower((isset($_POST["CourseName"]))?$_POST["CourseName"]:""))))); ?>
But I would like to use trim or something similar to remove the trailing whitespace rather than having extra hyphens.
Does anyone know how I can do this please?