dongping1689 2016-02-18 14:25
浏览 41

PHP从字符串中删除预定义的字符

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.

  • 写回答

1条回答 默认 最新

  • dongmuyuan3046 2016-02-18 14:46
    关注

    There might be many other things to take into consideration, but to answer yor question, you could do something like this:

    first check if there was a restart, then:

    $nodename =  explode("-", $nodename);
    $nodename = $nodename[0];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行