doujiao9574 2018-03-01 17:21
浏览 36
已采纳

从动态生成的字符串中删除最后一个字符

I a trying to remove the last character in my string. I have seen many examples to do that but all the methods are removing my all '&' but i want only my last character '&' to be removed.

My string is :

https://website.biz/get/#TYPE#? partner_id=XXXXXXXX&api_password=XXXXXXXX&mobile_no=#NUMBER#&operator_code=#OPERATORID#&amount=#AMOUNT#&partner_request_id=#ORDERID#&circle=10&recharge_type=NORMAL&

The complete API is dynamic. I want to remove the last string '&' from this string. The above line is coming from my PHP code :

<td><?php $parameter = fetchAPIparameter($v6['api_id'],$v6['operator_type']);
    foreach ($parameter as $v7) {
       $pks = ($v7['parameter'].'='.$v7['result'].'&'); echo ($pks);
    } ?></td>

I have used substr($pks,0,-1) and substr_replace($pks,"",-1) and rtrim and mb_substr but all are removing the complete '&' from my API. Can you please help me what mistake i am doing.

  • 写回答

3条回答 默认 最新

  • douxin2011 2018-03-01 17:27
    关注

    You need to build a string in a variable then remove the last character.
    The way you do it you remove the last character on each iteration which is not what you want.

    <td><?php 
    $parameter = fetchAPIparameter($v6['api_id'],$v6['operator_type']);
    $pks = "";
    foreach ($parameter as $v7) {
       $pks .= ($v7['parameter'].'='.$v7['result'].'&');  //here i am getting error
    } 
    echo substr($pks,0,-1);
    ?></td>
    

    .= means it will append to the string.
    substr will echo all but the last character.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样