5

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

Community
  • 1
  • 1
alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • 1
    You mean remove everything but the alphanumeric characters? (And do lower case?) – Jared Farrish Dec 13 '12 at 05:17
  • 1
    @Jared Farrish Yeah, and add the slashes between the words I guess – alexchenco Dec 13 '12 at 05:19
  • Whoops, I thought this was for Javascript. So... `'Zhongxiao Dunhua Sun'.toLowerCase().replace(/ /g, '-').replace(/[^a-z\-]/gi, '');` – Jared Farrish Dec 13 '12 at 05:21
  • @Jared Farrish ha that's for JavaScript? Thanks too. – alexchenco Dec 13 '12 at 05:24
  • @hakre - You seem a little cranky... `:)` That function is funky, but does cover the territory I was thinking it should (with special characters and etc.). – Jared Farrish Dec 13 '12 at 05:40
  • @alexchenco - I did a demo with some different possibilities: http://codepad.org/rJNSQmGJ Alix Axel's `oSlug()` in his linked (by @hakre) answer looks with my somewhat limited test to work the best. @leepowers version, while close, doesn't handle some special characters. Also, I reformatted Alex's `oSlug()` function so it is easier to read; see `nSlug()`. – Jared Farrish Dec 13 '12 at 06:39

4 Answers4

14
$s = ' Zhongxiao Dunhua Sun ';
$r = preg_replace('/\W+/', '-', strtolower(trim($s)));
echo("$r\n");
leepowers
  • 37,828
  • 23
  • 98
  • 129
3

PHP has a function for making strings conform to URL standards:

urlencode($mrt1);
sg-
  • 2,196
  • 1
  • 15
  • 16
  • 2
    The OP may not want to use `+` (for design reasons...). That is a reasonable suggestion, though. – Jared Farrish Dec 13 '12 at 05:32
  • 1
    Just this one single approach can have some (at least visually) horror-show outcomes: http://codepad.org/rJNSQmGJ So much for "pretty" urls... – Jared Farrish Dec 13 '12 at 06:45
2

You ca try this function also..

function create_seo_link($text) {
    $letters = array(
        '–', '—', '"', '"', '"', '\'', '\'', '\'',
        '«', '»', '&', '÷', '>',    '<', '$', '/'
    );

    $text = str_replace($letters, " ", $text);
    $text = str_replace("&", "and", $text);
    $text = str_replace("?", "", $text);
    $text = strtolower(str_replace(" ", "-", $text));

    return ($text);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Mrinmoy Ghoshal
  • 2,804
  • 1
  • 19
  • 17
0

str_replace() to change spaces to -, and strtolower() to change to lowercase.

$mrt1 = 'Zhongxiao Dunhua Sun';
$mrt1 = str_replace(' ','-',strtolower($mrt1));
Sean
  • 12,443
  • 3
  • 29
  • 47
  • I'm not quite sure this would cover all cases (such as special characters the OP might not want left in, like `*%$.?` etc.). It might not matter, but it is a big gap. – Jared Farrish Dec 13 '12 at 05:28
  • True, but based off his example, this simple example would work. If he has more stringent requirements, @leepowers' answer is a better solution. – Sean Dec 13 '12 at 05:31