yankeekid's avatar

str_slug strips everything after ampersand & hash tag

Curious if this is by design or if it's a bug.

I'm using the helper function str_slug (Support\Str.php) to create URL-friendly slugs when a page or post is created similar to the way Wordpress does. I noticed that any time an ampersand (&) or hash tag (#) is included, str_slug strips everything in the string after that character.

Example:

Input: Nuts & Bolts Returned: String: nuts

Input: Nuts # bolts Returned: nuts

I would expect the function to simply remove the "&" or "#" and return the string "nuts-bolts" but instead it seems to strip everything after the ampersand or hash tag. I've tried this with much longer strings and get the same result.

Anyone else have this issue?

It's not the same with other characters, as it replaces the "@" symbol with the word "at" and simply strips out other characters and leaves the remaining string intact.

I haven't tested all possible special characters, but these two are the ones that I've personally come across that seem to invoke this behavior.

Not sure if this is by design or if there is something else causing it.

Kind of annoying, though.

May have to rethink using this helper for slug creation and create a custom one.

0 likes
8 replies
Nash's avatar
Nash
Best Answer
Level 20

It's probably meant to remove/avoid query strings and anchors in the URL, e.g.

  • example.com?order=name&sort=asc
  • example.com#tab2
1 like
yankeekid's avatar

Yeah I had considered it, actually.

But it's such a simple thing that I just wrote my own helper function specifically for the URL slugs. I was mainly curious if there was a legitimate reason that it behaves this way. Figured this would be the place to ask.

yankeekid's avatar

@Nash Funny, though it doesn't do the same with the question mark (?).

Cronix's avatar

Why would someone be running a query string through a slug function though? It would destroy the data and parameters making the qs useless.

Nash's avatar

@Cronix Not necessarily run a query string through the function, but rather avoid that it happens by mistake...and to make sure that it simply isn't possible (in case someone actually does have a link or something in the string).

Something like "Haha, I love hashtags #smile" could make the page jump to an anchor with same ID (id="smile").

Although, it would probably be enough if it simply removed the unwanted characters instead of replacing everything that comes after them.

yankeekid's avatar

@Nash agreed. Doesn't really make sense to strip everything after. Maybe it is a bug after all, lol.

Please or to participate in this conversation.