dotn30471 2016-03-08 21:37
浏览 40
已采纳

php strpos offset输出不同

What is it that causes this difference?

echo strpos('abcdef abcdef', 'a', 0); //output= 0
echo strpos('abcdef abcdef', 'a', 1); //output= 7
echo strpos('a b- c a b* c a b+ c', 'c', 0); //output= 5 
echo strpos('a b- c a b* c a b+ c', 'c', 1); //output= 5 (But why? Is is must be 12)
echo strpos('a b- c a b* c a b+ c', 'c', 6); //output= 12
  • 写回答

2条回答 默认 最新

  • dpbl91234 2016-03-08 21:43
    关注

    The 3rd argument is the starting location, not the number of found characters to skip. e.g.

    strpos('...', 'a', 0)   option #1
    strpos('...', 'a', 1)   option #2
    
    abcdef abcdef
    0123456789111
    ↑↑        012
    ││
    └┼─ option #1: start at position 0('a'), and boom, find an 'a'
     └─ option #2; start at position 1('b'), find 'a' at position 7
    

    So for your a b- c a b* c a b+ c string, the second version only skips 1 character, leaving the first c to be found at position 5:

    a b- c a b* c a b+ c
    01234567891111111111
    ↑↑    ↑   0123456789
    ││    │
    └┼────┼─ start at position 0, first C at position 5
     └────┼─ start at position 1, first C still at 5
          └─ start at position 6, first C now at 12
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号