Site icon Anthony Carbon

Convert string with unwanted character to slug, proper name, etc.

How Convert string with unwanted character to slug, proper name, etc.? Try this code using http://phptester.net/.

// Example 1
function cleantitle($string){
   $string = str_replace(' ', '-', $string);
   $string = preg_replace('/[^A-Za-z0-9\-]/', '-', $string);
   return preg_replace('/-+/', ' ', $string);
}

$str_1 = 'Menstruation_Sisters_-_14';
echo cleantitle($str_1); // output will be "Menstruation Sisters 14"

// Example 2
function cleantitleslug($string){
   $string = str_replace(' ', '-', $string);
   $string = preg_replace('/[^A-Za-z0-9\-]/', '-', $string);
   return preg_replace('/-+/', '-', $string); // change the "-" to your custom separator
}

echo strtolower(cleantitleslug($str_1)); // output will be "menstruation-sisters-14"

Happy Coding 🙂

Exit mobile version