January 21st, 2013, 05:48 PM
Help needed to remove last portion of a string
I'm a total n00b to regex and I have been working for hours on this and cannot get anything to work as I'd like.
I need to remove the last part of a string that occurs after (and including):
The remaining characters are all lower case and underscores.
needs to be:
My first thoughts are to use preg_replace() with an empty string for the replace part but really struggling with the expression to use. Any help is greatly appreciated
January 21st, 2013, 06:15 PM
January 21st, 2013, 08:15 PM
This is one way:
$test = "elaphe_guttata_x_elaphe_guttata_emoryi";
$test = preg_replace('/(.+)_x_.*/', '\1', $test);
January 22nd, 2013, 04:05 PM
thanks requinix, I didn't think of that, it worked like a charm