用数字替换字符

There is this code that I am doing

Example a string of this value

z12z

I want to generate it

0120
0121
0122
... until 0129

then

1120
1121
1122

... until 1129

until 9129 , its sort of like two four loop, but I got no idea how to implement this.

and the issue is z can be anywhere and it can be zzzz

where it will be

0000 until 9999

or it could also be z0z0, z could be anywhere. What kind of method should I use for such.

Thanks!

I am doing it with php

for every occurance of letter 'z' , i will need do a for loop to generate the possible number, from 0 to 9, you can say z is a for loop for 0 to 9, e.g z555 will yield 0555,1555,2555,3555,4555,5555,6555,7555,8555,9555 , issue is z can occur with a possibility of 0 to 4, like z555 , zz55,zzz5, zzzz, and z position is random , I need generate the possible z number output

z position could be 55z5 , 5z55 , 5zz5 . its does not have a fix position.

<?php

$numbers = array();
for ($i = 0; $i <= 9; $i++){
    for ($j = 120; $j <= 129; $j++){
        $numbers[] = $i . $j;
    }
}

print_r('<pre>');
print_r($numbers);

A better answer that take the z char is:

<?php
function strReplaceNth($search, $replace, $subject, $nth)
{
    $found = preg_match_all('/' . preg_quote($search) . '/', $subject, $matches, PREG_OFFSET_CAPTURE);
    if (false !== $found && $found > $nth) {
        return substr_replace($subject, $replace, $matches[0][$nth][1], strlen($search));
    }

    return $subject;
}

function cleanup($numbers, $char)
{
    $tmp = array();
    for ($i = 0; $i < count($numbers); $i++){
        if (strpos($numbers[$i], $char) === false){
            $tmp[] = $numbers[$i];
        }
    }

    return $tmp;
}

function generateNumber($numbers, $char)
{
    if (!is_array($numbers)){
        if (strpos($numbers, $char) === false){
            return array($numbers);
        } else {
            $tmp = $numbers;
            $numbers = array();
            for ($j = 0; $j <= 9; $j++){
                $numbers[] = strReplaceNth($char, $j, $tmp, 0);
            }
            return generateNumber($numbers, $char);
        }
    } else {
        for ($i = 0; $i < count($numbers); $i++){
            if (strpos($numbers[$i], $char) === false){
                return cleanup($numbers, $char);
            } else {
                $numbers = array_merge($numbers, generateNumber($numbers[$i], $char));
            }
        }
        return generateNumber($numbers, $char);
    }
}

function getCharPos($string, $char)
{
    $pos = array();
    for ($i = 0; $i < strlen($string); $i++){
        if (substr($string, $i, 1) == $char){
            $pos[] = $i;
        }
    }

    return $pos;
}

$string = 'z12z';
$char = 'z';
$occurences = getCharPos($string, $char);
$numbers = array();

if (count($occurences) > 0){
    $numbers = generateNumber($string, $char);
} else {
    $numbers[] = $string;
}

print_r('<pre>');
print_r($numbers);die();