Removing the last character of a string IF it is $variable

Posted by KeenLearner on Stack Overflow See other posts from Stack Overflow or by KeenLearner
Published on 2011-01-16T07:38:35Z Indexed on 2011/01/16 7:53 UTC
Read the original article Hit count: 169

Filed under:
|

Hi there,

I've made a little script to convert titles into url friendly things.

ie:

'I am a title'

becomes

'I_am_a_title'

My script basically goes through and turns spaces, apostrophes, commas etc etc into an underscore.

The problem is, sometimes my url's end up like this:

'i_am_a_title_'

with a trailing underscore...

So i figure, add a little bit to go through and search to see if the last character is an underscore on the final result, and if it is, then swap it.

I looked into the strrchr() function but I seem to be hitting a wall of my own understanding.

How is this sort of thing accomplished?

© Stack Overflow or respective owner

Related posts about php

Related posts about string-manipulation