alexchenco alexchenco - 15 days ago 5
PHP Question

Turning a name into a URL-friendly string in PHP?


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)?

Answer
$s = 'Zhongxiao Dunhua Sun';
preg_replace('/\W+/', '-', strtolower($s));
echo("$s\n");