josephmtinangi's avatar

Is there a Laravel helper to reverse a slug?

For example if I have a slug called some-post and when I pass it to that helper it will return Some Post

0 likes
22 replies
jbowman99's avatar

@josephmtinangi

good question, I've had to just reverse engineer whatever was being passed in as a slug, to return it to the string i wanted.

using

preg_split() - Split string by a regular expression str_split() - Convert a string to an array

if someone has a cleaner quicker way I'm all ears

1 like
patricpoba's avatar

try this.

$var = 'some-post';

echo ucwords(str_replace('-', ' ', $var)); 
10 likes
liamvictor's avatar

I did this assuming that the Str::title will be more robust over time

{{ Str::title(str_replace('-', ' ', $slug)) }}

12 likes
martinbean's avatar

@liamvictor Dude, the last post was two years ago. Why have you dug up and replied to such an outdated thread?

liamvictor's avatar

Because this result turns up when you do a search and my comment might help someone else.

22 likes
Magalliu's avatar

Helped me also... thanks for answering to this threads!

2 likes
dyoung's avatar

Still appearing in results and helping people :) cheers mate

2 likes
JussiMannisto's avatar

@ryanmortier

Why So Toxic This Just Helped Me Too From Google

I'm glad it helped you. It's not very good advice, though. Slugs can't be "reversed" due to loss of information.

1 like
martinbean's avatar

@ryanmortier The irony at digging up an old thread to call me “toxic” for calling out someone for digging up an old thread…

My stance on the original problem hasn’t changed. Even after all these years. You can’t reliably get the original text from a slug.

Consider a post with the title “Donald Trump: America’s Next President”. That’s going to get converted to a slug like donald-trump-americas-next-president. Great. So now how are you going to get back the original post title with the correct capitalisation, and punctuation such as the apostrophe and colon? You can’t. That information is gone.

aeadedoyin's avatar

Let's add to @liamvictor's ->answer

And go all Laravel helper on this.

{{Str::of($slug)->replace('-', ' ')->title()}}

#archaeologists 🤘

4 likes
martinbean's avatar

@cactus It’s not going to put back in any punctuation that will have been converted to dashes, though.

Consider “laravels-the-best-php-framework”. You’re going to get back something like “Laravels the Best Php Framework” which will not have been the original title.

1 like
bentz's avatar

@cactus @martinbean thanks to you both - I find myself in a situation where Str::headline will be useful regardless of the punctuation thing!

bentz's avatar

With a framework like Laravel that constantly updates over time, forum post's like this are super valuable as they appear in search results and remain relevant. Updating them over time is helpful to those who stumble upon them in the future. It's ok and even encouraged to "necro" posts like these :)

Snapey's avatar

@bentz and some appear to promote outdated or just plain bad advice

Please or to participate in this conversation.