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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。