2017-06-12 30 views
-1

这个替换的最佳解决方案是什么?快速和优化PHP preg替换为干净的网址

我的链接:

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

我要干净:

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

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

编辑:

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

$str2 = "Country Music (1990)";

替换到:

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

$str2 = "Country-Music-1990";

谢谢。

+2

可能的复制(https://stackoverflow.com/questions/5305879/automatic-clean-和搜索引擎友好的URL,蛞蝓) –

回答

1

尝试[自动清洁和搜索引擎友好的URL(蛞蝓)的这个

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

参考 - Automatic clean and SEO friendly URL (slugs)

+0

工作。完美解决方案谢谢 – grizzly