dongyuhui0418 2017-04-03 17:24
浏览 17
已采纳

PHP:替换完全字符串

I am trying to replace exact string with some other string but all what i have been trying not really work as i need.

Here is my string: http://test.com/?plus=&quotea=&quoteb=

I need to replace quotea= to quotea=cat so that quoteb want change.

I tried:

preg_replace('/quotea=/', 'quotea='. $parmeter.'', $str, 1);

str_replace( 'quotea=', 'quotea='. $parmeter.'', $str, $i );

After i run one of the code the result is:

http://test.com/?plus="ea=cat"eb=

What i am missing here?

  • 写回答

2条回答 默认 最新

  • duanquan1243 2017-04-03 17:38
    关注

    You could use preg_replace to change the value of quotea regardless of whether it has a value or not:

    $str = 'http://test.com/?plus=&quotea=&quoteb=';
    $rep = 'cat';
    
    echo preg_replace('/quotea=[^&]*&quoteb/', "quotea=$rep&quoteb", $str);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器