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.