0

what is best solution for this replacing? fast and optimized

my link:

https://example.com/event/123/Kinda Don't Care2 Feat. Jaba/Country Music (1990)

I want clean to:

https://example.com/event/123/Kinda-Dont-Care2-Feat-Jaba/Country-Music-1990

preg_replace('???????', '-', $strUrl);

Edit:

$str1 = "Kinda Don't Care2 Feat. Jaba";

$str2 = "Country Music (1990)";

replace to :

$str1 = "Kinda-Dont-Care2-Feat-Jaba";

$str2 = "Country-Music-1990";

thanks.

Farzad
  • 1,975
  • 1
  • 25
  • 47
  • 2
    Possible duplicate of [Automatic clean and SEO friendly URL (slugs)](https://stackoverflow.com/questions/5305879/automatic-clean-and-seo-friendly-url-slugs) – scrowler Jun 12 '17 at 05:02

1 Answers1

1

try this

$string = "example.com/event/123/Kinda Don't Care2 Feat. Jaba/Country Music (1990)";

    // remove all non alphanumeric characters except spaces
    $clean =  preg_replace('/[^a-zA-Z0-9\s]/', '', strtolower($html)); 

    // replace one or multiple spaces into single dash (-)
    $clean =  preg_replace('!\s+!', '-', $clean); 

    echo $clean;

ref- Automatic clean and SEO friendly URL (slugs)

Shahmee
  • 299
  • 1
  • 2
  • 16