Traverse string from end and find the number stored at the end . x is used to store power of 10.
$ num = 0 ; $ x = 1 ; $ i = $ n - 1 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( '0' <= $ str [ $ i ] && $ str [ $ i ] <= '9' ) { $ num = ( $ str [ $ i ] - '0' ) * $ x + $ num ; $ x = $ x * 10 ; if ( $ num >= $ n ) return false ; } else break ; }