ak85
ak85

Reputation: 4264

regular expression to remove part of string

I am trying to instert an integer into a form. Users are asked to input there promo number which is in the format 12.2-9999. I only want to insert 9999 into the column in my database but I am getting -9999 in the columns. I am not sure if I should use preg_replace or something else here?

Upvotes: 0

Views: 140

Answers (2)

nickb
nickb

Reputation: 59699

Just use explode(), like so:

list( , $promo) = explode( '-', '12.2-9999');

Now $promo will contain 9999.

Upvotes: 4

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74202

The following regular expression will check for the last - and capture the digits to the end (which as per the example is 9999):

/-(\d+)$/

Upvotes: 2

Related Questions