dty98339 2017-11-01 20:41
浏览 8
已采纳

如何读取整个子字符串

This was difficult to put into a title.

Essentially what I want to do is, when I get some random binary code i need certain number patterns be converted into a declared symbol.

For example :

input: 11100011111 Output: ABF

So essentially

  • 111 = A, 000 = B, 11111 = C, 1111 = D

Note!That the output has to correspond in order so if it started with 111 the output hast to start with A(declared symbol for said pattern). There's a D there also essentially the first four characters of C is the same as D but it must output the one most similar.

First i tried to put this into a for loop so, check string characters, if there's a 1 echo one and vice versa

for ($i=0; $i < $getInput[$i] ; $i++) { 
    if ($getInput[$i] == 1) {
        echo "ONE";
    } elseif ($getInput[$i] == 0) {
        echo "ZERO";
    }   
}

this was just for testing, essentially in this theory i can check if if there is a consecutive pattern so 000 it will refer to an array with different patterns already assigned and then output the symbol whatever was assigned to that pattern.

I also tried using foreaches but due to inexperience...

I did some research and found this topic: How do I check if a string contains a specific word?

which kinda helped since with this code

$a = '1110001111';

if (strpos($a, '1111') !== false) {
    echo 'D';
}

It outputs the character in right order, problem with this approach is that well it kinda means id need to write out if statements repetitively, and secondly if there were to be 1111 in the input, it would take the part 111 and 1111 as two different strings and output both of them

EDIT:

Essentially what i just thought is, i actually need to compare 4 digits to different patterns, i can put the string into an array split it into 4's and then compare with another array which holds the declared patterns (1111 = a etc..), so in a 4 digit binary 0000 there can be about 17 different patterns therefor no need for anything above >4

Then again if there were an odd number of 1's and 0's there'd be some left off. Then these should just be outputted as is. so if there were 1010110 = A110 since A is declared as a pattern of 1010

  • 写回答

2条回答 默认 最新

  • douhao2548 2017-11-01 21:02
    关注

    You can use str_split to split the string in chunkes of four digits.
    Then I use an associative array with replacements.

    I loop the match array and echo replacement character.

    $str = "111000111111";
    //preg_match_all('/(.)\1*/', $str, $match);
    $digits = str_split($str, 4);
    $replace = array("1110" => "A", "0011" => "B", "1111" => "C");
    
    Foreach($digits as $val){
         Echo $replace[$val];
    }
    

    https://3v4l.org/QfQQ8

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测