We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Remove underscore from segment variable.

How Do I?

Trevzilla's avatar
Trevzilla
15 posts
11 months ago
Trevzilla's avatar Trevzilla

I’m using a segment variable to display a title on our website. However, in the URL the segment I’m pulling from sometimes has an underscore. Is there a way I can display the contents of {segment_2} with a space instead of an underscore?

e.g {segment_2} contains: “some_text” but I just want to display “some text” on the website.

       
mithra62's avatar
mithra62
59 posts
11 months ago
mithra62's avatar mithra62

You really only have the option for dashes - or underscores _ when it comes to URL segments. You can set this using the word_separator config variable, like $config['word_separator'] = 'dash';

That said, even if you COULD use spaces as a separator, there isn’t a single browser on the planet that would allow that, since a space isn’t a valid URL character; instead, every browser will convert every space to the encoded value of %20. So a URL like /my/url title with spaces would be converted to /my/url%20title%20with%20spaces instead.

       
Trevzilla's avatar
Trevzilla
15 posts
11 months ago
Trevzilla's avatar Trevzilla

I’m sorry if that wasn’t clear. The URL has the underscore in it. I don’t want to create a new URL. I want to pull {segment_2} FROM the URL and display it on the website.

<html opening tag>{segment_2}</html closing tag>

But when I do that, I get something that looks like this:

My_Webpage_Title

But I want it to look like this:

My Webpage Title

Again, this has nothing to do with the url itself having spaces. That’s not what I’m going for.

       
mithra62's avatar
mithra62
59 posts
11 months ago
mithra62's avatar mithra62

Oh! Missed that. You can use the replace variable modifier, like so:

{segment_2:replace find='_' replace=' '}

The docs have more details if you’re curious

👏 1
       
Trevzilla's avatar
Trevzilla
15 posts
11 months ago
Trevzilla's avatar Trevzilla

Amazing! Thank you! That did the trick.

Also, thank you for the docs link. I had browsed through them, but didn’t know where to look either. I’ll be sure to review your link on that. Seems like that replace function could be super powerful!

       
fortuneriches's avatar
fortuneriches
1 posts
9 months ago
fortuneriches's avatar fortuneriches

I’m dying to have Five Nights at Freddy’s with this super cool.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.