How do I replace multiple characters with the same number of characters with a regular expression?

后端 未结 3 1802
旧巷少年郎
旧巷少年郎 2021-01-20 23:58

I\'ve got the following source:

011011000010011011&         


        
3条回答
  •  礼貌的吻别
    2021-01-21 00:22

    Try this here

    0110110000100110110110';
    $pattern = '/(?<=)( *?)[10](?=.*?<\/font>)/';
    $replacement = '$1 ';
    while (preg_match($pattern, $string)) {
            $string = preg_replace($pattern, $replacement, $string);
    }
    echo $string;
    

    I use a positive look behind (?<=) to search for the color part. And a positive look ahead (?=.*?<\/font>) for the end.

    Then I match the already replaced spaces and put them into group 1 and then the [10].

    Then I do a while loop until the pattern do not match anymore and a replace with the already replaces spaces and the new found space.

提交回复
热议问题