Search code examples
phptrailing

Remove all 0s except trailing 0s from a string


There are a lot of questions on removing leading and trailing 0s but i couldn't find a way to remove all 0s except trailing 0s (leading 0 or in any other place other than the end).

100010 -> 110  
010200 -> 1200  
01110  -> 1110

any suggestions ?


Solution

  • Try

    echo preg_replace('/0+([1-9])/', '$1', $str);