Search code examples
phpregexstringtrim

Trim letters from the end of a string


I am trying to write a function that will trim all the letters off the end of a string, upto the first number it hits.

I have child product names like, item101S, item101xxx, etc. and I want to output only the parent product name: item101.

Not sure if I should be using regular expressions or !is_numeric() or what.


Solution

  • php > $s="item101xxx";
    php > $pattern="/[^\d]*\d+/";
    php > echo preg_match($pattern, $s, $m);
    1
    php > print_r($m);
    Array
    (
        [0] => item101
    )