framework icon indicating copy to clipboard operation
framework copied to clipboard

Handling spaces in slug generation

Open perkola opened this issue 12 years ago • 12 comments

Currently the slug generator in the Illuminate\Support\Str helper produces different output depending on whitepaces in the input string.

For example:

Str::slug('foo/bar baz');      // outputs 'foobar-baz'
Str::slug('foo / bar baz');    // outputs 'foo-bar-baz'

The first line results in the removal of the slash while the second lines slash sorrounded with whitespaces results in a delimiter: foobar-baz vs foo-bar-baz.

My proposal is to treat slashes in slugs the same way as whitespaces: by replacing with the delimiter. That would result in:

Str::slug('foo/bar baz');      // outputs 'foo-bar-baz'
Str::slug('foo / bar baz');    // outputs 'foo-bar-baz'

Notice now how both lines produces the same output unlike before.

Summary: I would like the Illuminate\Support\Str helper to transform slashes into the delimiter and not remove them entirely to avoid confusion when extra whitespaces are present around the slash.

perkola avatar Jul 21 '14 10:07 perkola

I don't really see a difference between the two code samples, you sure they're right ?

Anahkiasen avatar Jul 21 '14 10:07 Anahkiasen

The code is the same, he is talking about the output ;) foobar-baz vs foo-bar-baz (1st example)

barryvdh avatar Jul 21 '14 10:07 barryvdh

Yes, sorry for not beeing specific, I have updated my opening post.

perkola avatar Jul 21 '14 11:07 perkola

I just did some more testing and it turns out that the same result occurs with every non-alphanumerical character:

Str::slug('foo , bar');     // outputs 'foo-bar'
Str::slug('foo, bar');      // outputs 'foo-bar'
Str::slug('foo ,bar');      // outputs 'foo-bar'
Str::slug('foo,bar');       // outputs 'foobar'

Str::slug('foo ! bar');     // outputs 'foo-bar'
Str::slug('foo! bar');      // outputs 'foo-bar'
Str::slug('foo !bar');      // outputs 'foo-bar'
Str::slug('foo!bar');       // outputs 'foobar'

It seems that any character, punctuation mark or other special character, with no adjacent whitespace will be removed entirely: foo,bar becomes foobar. Is there any reason for punctuation marks and other special characters with no whitespace adjacent to be removed entirely instead of converted to the delimiter?

I would like punctuation marks and special characters with no adjacent whitespace to be treated as the other strings (where there is an adjacent whitespace present).

perkola avatar Jul 21 '14 11:07 perkola

I think it makes more sense for anything that is not alphanumeric to be converted to the delimiter. Would probably be easy to change unless this is expected behaviour for some reason.

RyanNielson avatar Jul 21 '14 12:07 RyanNielson

This would be a major breaking change though so would be for 4.3 at minimum.

Anahkiasen avatar Jul 21 '14 12:07 Anahkiasen

@Anahkiasen Oh for sure, it probably couldn't go into 4.2 as some people might depend on certain behaviour from the function.

RyanNielson avatar Jul 21 '14 12:07 RyanNielson

@RyanNielson not to mention broken URLs, duplicated content, etc.

bworwa avatar Jul 21 '14 13:07 bworwa

I hope people don't rely on the slug output.. You should generate a slug and store that, not regenerate it everytime.

barryvdh avatar Jul 21 '14 13:07 barryvdh

@barryvdh I'm sure most do, but maybe some people rely on being able to use things like / if they want separators in their strings but not in their slugs. I have no idea.

RyanNielson avatar Jul 21 '14 13:07 RyanNielson

I might add that the slug dies if we use Cyrillic or other non ASCI characters. $title = static::ascii($title); Оur slug definitely needs more love.

goranovs avatar Nov 29 '14 23:11 goranovs

We're open to pull requests if anyone has a proposal.

GrahamCampbell avatar Jan 25 '15 14:01 GrahamCampbell