import re


def php_sprintf(_format, *args):
    """
    >>> num = 5
    >>> location = 'tree'
    >>> php_sprintf('There are %d monkeys in the %s', num, location)
    'There are 5 monkeys in the tree'
    >>> php_sprintf('The %2$s contains %1$d monkeys', num, location)
    'The tree contains 5 monkeys'
    >>> php_sprintf('''%'.9d''', 123)
    '......123'
    >>> php_sprintf('''%'09d''', 123)
    '000000123'
    >>> php_sprintf('The %2$s contains %1$04d monkeys', num, location)
    'The tree contains 0005 monkeys'
    >>> money = 68.75 + 54.35
    >>> php_sprintf('%01.2f', money)
    '123.10'
    """

    def _fix(m):
        n = m.group('argnum') or ''
        w = m.group('width') or ''
        f = m.group('flags') or ''
        fc = m.group('fillchar') or ' '
        fc = fc[1:]

        p = m.group('precision') or ''
        s = m.group('spec') or ''
        if len(p) > 0:
            p = '.' + p
        sign = ''
        align = ''

        if f == '-':
            align = '<'
        else:
            sign = f

        if fc != '' and align == '':
            align = '>'

        fspec = f'{fc}{align}{sign}{w}{p}{s}'
        if len(fspec) > 0:
            fspec = ':' + fspec

        if len(n) > 0:
            n = str(int(n) - 1)
        if s == 's':  #  if string is specified, convert to string!
            return f'{{{n}!s{fspec}}}'
        return f'{{{n}{fspec}}}'

    _format = re.sub(
        '%(?P<argnum>\d+)?\$?(?P<flags>[-+0])?(?P<fillchar>\'[\w\.])?(?P<width>\d+)?\.?(?P<precision>\d+)?(?P<spec>[%bcdeEfFgGosuxX])', _fix, _format)

    return _format.format(*args)