douchan6512 2014-10-19 08:25
浏览 55
已采纳

用数字替换字符

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.

  • 写回答

1条回答 默认 最新

  • dongtan7639 2014-10-19 08:35
    关注
    <?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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染