douyi1944 2017-04-17 16:18
浏览 26
已采纳

PHP代码背后的逻辑

I'm having trouble understanding the logic behind the following code:

Initial code

            <?php
            $output_arr=array();
            $handle = fopen("../etc/init.d/bandwidthlimiters/rulestest", "r");
            if ($handle) {

                $i=1;
                while (($line = fgets($handle)) !== false) {

                $test_string=$line;
                $test_string=str_replace(" htb rate ","@@",$test_string);
                $test_string=str_replace("kbit ceil ","@@",$test_string);

                $tmp = explode("@@", $test_string);
                array_push($output_arr,$tmp[1]);
                $i++;
               }

                fclose($handle);
            } else {
                // error opening the file.
            } 

            echo $output_arr[5]
            ?>

At the moment echo $output_arr[5] provides a printed value of "5" when taken from the file source listed. File contents shown below, under "New Output".

New Output

Instead of looking for values between " htb rate " and "kbit ceil", I would like it to grab results based on the following terms: " ceil " and "kbit"

tc class add dev br-lan parent 1:1 classid 1:2 htb rate 1kbit ceil 11111kbit
tc class add dev br-lan parent 1:1 classid 1:3 htb rate 2kbit ceil 22222kbit
tc class add dev br-lan parent 1:1 classid 1:4 htb rate 3kbit ceil 33333kbit
tc class add dev br-lan parent 1:1 classid 1:5 htb rate 4kbit ceil 44444kbit
tc class add dev br-lan parent 1:1 classid 1:6 htb rate 5kbit ceil 55555kbit

By using the sample lines above, the new result should retrieve "55555", instead of the original "5"

Modified version

I have tried a modification, as shown below:

                $test_string=str_replace(" ceil ","@@",$test_string);
                $test_string=str_replace("kbit","@@",$test_string);

However, this does not return any result. For future use, I would like to be able to manipulate this further to return different results again, in which case, any explanation of how to use this method would be appreciated.

  • 写回答

1条回答 默认 最新

  • duanluan8390 2017-04-17 16:32
    关注

    Your modification is resulting in this value of $test_string:

    tc class add dev br-lan parent 1:1 classid 1:6 htb rate 5@@@@55555@@
    

    Notice that there are two @@ sequences before the field that you want to retrieve. The first comes from replacing kbit, the second comes from replacing ceil. When you explode it, you get:

    array(4) {
      [0]=>
      string(57) "tc class add dev br-lan parent 1:1 classid 1:6 htb rate 5"
      [1]=>
      string(0) ""
      [2]=>
      string(5) "55555"
      [3]=>
      string(1) "
    "
    }
    

    So 55555 is in $tmp[2], not $tmp[1].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写