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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里