douqiao7958 2018-06-08 15:16
浏览 41
已采纳

根据字符串php中的特定单词获取字符串的特定部分

I have a string, who contains multiple values. A new line is recognised as | symbol. The values between | belong together.

Now, I am trying for example if IKK and Homo sapiens are present in the line, to return the other values in that line (96, NC_000001 , IKK , inhibitor of nuclear factor, IKK = IKKI).

I use explode function for separating the string based on | symbol. But if I use the explode function again now based , symbol. I got again an array. But if I now would look for homo sapiens & IKK and want to return the other values from that same line, I got stuck.

I have the following string:

    $var = 'IKK ,Homo sapiens,96, NC_000001 , IKK , inhibitor of nuclear factor, 
    IKK = IKKI |GAL4 ,Saccharomyces cerevisiae,85588,NC_00114 , GAL4 ,0, GAL81
    |TRP ,Influenza HA,0,0,0,0,0|TES,Influenza HA,5,0,TES,0,0';

My code:

function terms($result){
    $result = (explode("|",$result));
    $final = array_unique($result);
    return $final;
}
$test = terms($var);
foreach ($test as $key => $value){
    echo "key: $key -- value: $value"."<br \>
"; 
    $res = explode(",",$value);
    foreach ($res as $v){
        echo "tester: >> $v"."<br \>
"; 
    }
}

I probably should end up with something like this. But I don't find a way to get there. Is it even possible to get this structure?

if ($d[1] == "Homo sapiens" && $d[0] == "IKK"){
    $k = $d[2] # 96
    $s = $d[3] # NC_000001
 }

As end result I would like to save the values 96, NC_000001 , IKK , inhibitor of nuclear factor, IKK = IKKI separately again and again in a separate variable. $number = 96 $acc = NC_000001

  • 写回答

5条回答 默认 最新

  • dongzhang6021 2018-06-08 15:58
    关注

    Due to white-spaces involved, I think you are unable to compare values. Please try this :

    <?php
    
    $var = 'IKK ,Homo sapiens,96, NC_000001 , IKK , inhibitor of nuclear factor, 
        IKK = IKKI |GAL4 ,Saccharomyces cerevisiae,85588,NC_00114 , GAL4 ,0, GAL81
        |TRP ,Influenza HA,0,0,0,0,0|TES,Influenza HA,5,0,TES,0,0';
    
    function terms($result){
        $result = (explode("|",$result));
        $final = array_unique($result);
        return $final;
    }
    
    
    function compare_terms($res){
        $d = array_map('trim',$res);
        if ($d[1] == "Homo sapiens" && $d[0] == "IKK") {
            $k = $d[2] ; 
            echo $k ."<br \>
    "; // 96
            $s = $d[3] ; 
            echo $s ."<br \>
    "; // NC_000001
        }
    }
    
    
    $test = terms($var);
    foreach ($test as $key => $value){
        echo "key: $key -- value: $value"."<br \>
    "; 
        $res = explode(",",$value);
        compare_terms($res);
    }
    

    Here in the compare_terms function, we have applied trim function on each value of array by using : $d = array_map('trim',$res);

    Now we can compare values accordingly

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

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂