3

I would like to construct a slug starting from the question title.
It would be useful to apps that want to show the original link to the question in a human readable way.

On StackPrinter i have this feature-request:

Suggestion: include the original question URL in prominent fashion near the top of the printout. That would be a good reference from the printed page back to the online version.

I'm aware that StackPrinter: The Stack Exchange Printer Suite works the same.

EDIT:
this will be fixed with the Link Fields feature of the new 2.0 API.

2
  • Instead of editing an "answer" into the "question", how about adding an actual answer post? (: Dec 27, 2011 at 20:23
  • because I'm not sure of the answer :); I've found the Link Fields feature proposal in the draft but not in the actual documentation available here api.stackexchange.com/docs Dec 27, 2011 at 21:42

3 Answers 3

3

the 'slug' is a cleaned title hyphen delimited and truncated to some length which is easily discoverable by measuring a few 'slugs' that are obviously truncated.

and in reality, you can place any thing in that path position, or nothing at all if there are no following path positions

by cleaned i mean that non ascii alpha numeric characters are removed and spaces are collapsed into hyphens.

1
  • yep, i knew that.. but too lazy in reverse engineering the Stack Exchange solution :). Jul 6, 2010 at 8:22
3

The slug seems to be limited to 81 characters. Here is a javascript version of slugify algorithm:

function slugify(str) {
    var stripped = stripVowelAccent(str);
    return stripped.replace(/[^A-Za-z0-9 ]/g, "").replace(/[ ]+/g, "-").toLowerCase().substr(0,81);
}

/* (C)Stephen Chalmers
* Strips grave, acute & circumflex accents from vowels
* Adjusted by Igor Zevaka to strip more accented characters
* http://bytes.com/topic/javascript/answers/145532-replace-french-characters-form-input
*/

function stripVowelAccent(str) {
    var s = str;

    var rExps = [/[\xC0-\xC4]/g, /[\xE0-\xE5]/g,
/[\xC8-\xCB]/g, /[\xE8-\xEB]/g,
/[\xCC-\xCF]/g, /[\xEC-\xEF]/g,
/[\xD2-\xD6]/g, /[\xF2-\xF6]/g,
/[\xD9-\xDC]/g, /[\xF9-\xFC]/g];

    var repChar = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u'];

    for (var i = 0; i < rExps.length; i++)
        s = s.replace(rExps[i], repChar[i]);

    return s;
}
2
  • 1
    good post. i think (C)Stephen Chalmers thinks a bit too much of himself. I think (c) should be reserved for non-trivial code, which this is not. To (c) something that is trivial with really only a few correct implementation possibilities is like saying that you own the internet. in this case, i wouldn't even bother retyping it. I would just paste the body into my slugify method. but that's just me. i have only been doing this 20+ years, what do i know? Jul 7, 2010 at 0:06
  • 1
    Attribution is a double-edged sword, isn't it? He insisted on putting up a copyright notice on trivial code, i reposted it, now we all know he's a bit of a wanker :) Jul 7, 2010 at 1:11
0

Can't this easily be constructed by appending a '/' and the title from the question?

In PHP:

$question_url .= '/' . preg_replace('/[^\w_]/','',str_replace(' ','_'$question->title));
2
  • 3
    It's actually using '-' as the word separator, but I agree that you can just as easily come up with your own version. It doesn't actually matter what you put after the ID, it just gets ignored anyway.
    – codeka
    Jul 6, 2010 at 3:52
  • Well if you want to get technical, yes... but my snippet was just an example. Jul 7, 2010 at 6:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .