Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex - how to remove the last character in a line 1

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB
if i have a string like this...

a bit of text here, and a bit of text there, and some more, just here

what regex would i use to remove the last comma? there could be any amount of commas (i wouldn't know how many), but i would like to remove (replace) the last one.
 
Hi

Like this ?
PHP:
[navy]$str[/navy][teal]=[/teal][green][i]'a bit of text here, and a bit of text there, and some more, just here'[/i][/green][teal];[/teal]
[b]echo[/b] [COLOR=darkgoldenrod]preg_replace[/color][teal]([/teal][green][i]'/,([^,]*)$/'[/i][/green][teal],[/teal][green][i]'{HERE WAS THE LAST COMMA}\\1'[/i][/green][teal],[/teal][navy]$str[/navy][teal]);[/teal]
Code:
a bit of text here, and a bit of text there, and some more{HERE WAS THE LAST COMMA} just here

Feherke.
 
nice one feherke,

say i knew which comma i wanted to remove and wanted to remove the 2 one.... how would i modify the code?
 
Hi

Not in a pleasant manner.
PHP:
[b]echo[/b] [COLOR=darkgoldenrod]preg_replace[/color][teal]([/teal][green][i]'/^([^,]*,[^,]*),/'[/i][/green][teal],[/teal][green][i]'\\1{COMMA}'[/i][/green][teal],[/teal][navy]$str[/navy][teal]);[/teal]
By the way, some people prefers to split and join the string instead :
PHP:
[navy]$part[/navy][teal]=[/teal][COLOR=darkgoldenrod]explode[/color][teal]([/teal][green][i]','[/i][/green][teal],[/teal][navy]$str[/navy][teal]);[/teal]
[COLOR=darkgoldenrod]array_splice[/color][teal]([/teal][navy]$part[/navy][teal],[/teal][purple]1[/purple][teal],[/teal][purple]2[/purple][teal],[/teal][COLOR=darkgoldenrod]implode[/color][teal]([/teal][green][i]'{COMMA}'[/i][/green][teal],[/teal][COLOR=darkgoldenrod]array_slice[/color][teal]([/teal][navy]$part[/navy][teal],[/teal][purple]1[/purple][teal],[/teal][purple]2[/purple][teal])));[/teal]
[b]echo[/b] [COLOR=darkgoldenrod]implode[/color][teal]([/teal][green][i]','[/i][/green][teal],[/teal][navy]$part[/navy][teal]);[/teal]

Feherke.
 
i'd definitely go with the explode method.

the manual recommends against regex for simple patterns. i believe this is because of the overhead in loading the PCRE engine (which is loaded on the fly). I understand that php 6 changes this but have not played enough to know.

with these types of question I think it is often better for the OP to explain why they are trying to achieve this aim. being given a discrete problem to solve is easy (most of the time) but often the existence of the problem is because of an earlier architectural choice that is flawed and perhaps our input at that point will create a smoother path.
 
hey guys,

the second question is not immediate problem, but i remember coming up with the issue before wanting to remove a certain character or word that occurs either 2nd, 3rd, etc. it was mostly in urls.

thanks so much for your help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top