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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。