Page 1 of 1

Help with regex

Posted: Sat Aug 20, 2011 10:43 am
by dreamline
Hi All,
I'm not that good in regex so I was hoping someone could help me out with my following problem. Suppose I have the following strings:
test.test1.
test2.test3.test3.

What I want to achieve is a regex that replaces the first few strings up until the before last ., so that I will have test1. and test3. (including the dots at the end) to work with.

Thanks for any help on this. :)

Re: Help with regex

Posted: Sat Aug 20, 2011 12:50 pm
by Christopher
You want to use preg_match_all() with the PREG_OFFSET_CAPTURE flag. Remember that '.' is a wildcard in regex, so you want to search for '\.' Or you could just use string functions like:

Code: Select all

$str = substr($str, strrpos($str , '.', -1) + 1);

Re: Help with regex

Posted: Sat Aug 20, 2011 8:08 pm
by dreamline
Thanks Christopher, that'll do the trick. :)