fonction pour supprimer les espaces et autres caractères spéciaux

d'abord, on fait les déclaration tout en haut du php

function slugify(string $text): string
{
    $text = strtolower($text);
    $text = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $text);
    $text = preg_replace('/[^a-z0-9]+/', '-', $text);
    $text = trim($text, '-');
    return $text;
}

 

Utilisation dans une boucle par exemple =>

$chapterId = slugify($chaptertitle);