I have a script connecting to a mikrotik router and reading traffic stats from it.
Here is my code:
$y = 0;
foreach ($ARRAYD as $d) {
$nodename=$ARRAYD[$y]['target'];
//$nodename=trim($nodename);
$nodename=substr($nodename,7,-1);//
//$nodename=rtrim($nodename, "-1");
$traff_bytes=$ARRAYD[$y]['bytes'];
$byte_values = preg_split('/\//',$traff_bytes);
if ($debug==0) {
$fr = fopen($myfile.$nodename,"w");
fwrite($fr,$nodename." ");
fwrite($fr,time()." ");
fwrite($fr,$byte_values[0]." ");
fwrite($fr,$byte_values[1]."");
fclose($fr);
$y++;
}
}
The API response gives me output like <pppoe-0404>
as $nodename
then this bit strips it down $nodename=substr($nodename,7,-1);
to 0404
by stripping the first 7 and last 1 characters.
The problem occurs if there has been a cpe restart and $nodename
becomes 0404-1
. How do I strip -1
from the end if it appears?
I tried $nodename=rtrim($nodename, "-1");
but then all nodes ending with 1
would loose the last digit from name. 0401
would become 040
which is incorrect output.