dongya3627 2017-10-28 09:42
浏览 59
已采纳

PHP str_replace替换两个单词

My code is below..

echo $type = str_replace(array('q','h','f'),array('Quarter','Half','Full'),$_POST['type']);

here on the above line $_POST['type'] have These 3 values;

  1. $_POST type = 'q'
  2. $_POST type = 'h'
  3. $_POST type = 'f'

I want to replace

  • q with Quarter
  • h with Half
  • f with Full.

My Problem is when $_POST['type']='h',

The result of above code means value of $type came HalFull.

Why this is happening...

Is there any solution for this...?

Thanks

  • 写回答

3条回答 默认 最新

  • dongqiaogouk86049 2017-10-28 09:49
    关注

    It seems like it is replacing h with Half and then the last f of Half with Full

    May be if you can provide a regexp that only replaces if the source string is exactly one character, then it would work.

    Try this:

    echo $type = preg_replace(array('/^q$/i','/^h$/i','/^f$/i'),array('Quarter','Half','Full'),$_POST['type']);
    

    It works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(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仿真结果几乎一样