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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?