Search code examples
phpregexpreg-match

PHP: Using preg_match to trim characters proceeding '-'


I have an unserialized array, I'm attempting to modify a string in the array and remove characters proceeding and including the hyphen.

The string looks like this, it's length isn't always regular though 'SOMETEXT - 150 x 50'

I have attempted a regular expression but it does not work, this is my first attempt at creating a regular expression.

$item['options']['Size'] = preg_replace('/^[^-]*,\s*/', '', $item);

If anyone would mind pointing me in the right direction here that would be fantastic.


Solution

  • preg_replace('/-[^-]*$/', '', $item);
    

    This should do it for you.