Turning a name into a URL-friendly string in PHP?
php
Solution
$s = ' Zhongxiao Dunhua Sun ';
$r = preg_replace('/\W+/', '-', strtolower(trim($s)));
echo("$r\n");
- Replaces non-word characters with dashes
- Converts to lowercase
Problem
Possible Duplicate: URL Friendly Username in PHP? I have something like this: ``` 'mrt1' => 'Zhongxiao Dunhua Sun' ``` which will be included into an anchor link like this: ``` <a href="'. $mrt1 .'">'. $mrt1 .'</a> ``` I want the output to be something like this: ``` <a href="zhongxiao-dunhua-sun">Zhongxiao Dunhua Sun</a> ``` How to do it so that I can turn that name into a URL-frienly string (so I can place it in the href attribute)?