drob50257447 2011-08-19 23:40
浏览 50
已采纳

preg_match:应该什么都不匹配?

Is preq_match supposed to return true for testing nothing on something?

$needle = ""; 
$haystack = "test";
$result = preg_match("/" . $needle . "/i", $haystack);

I would expect $result to be 0, but it's 1.

Logically speaking, testing nothing on something should yield nothing ... how does the song go? Nothing from something leaves nothing ... got to have something ... if you want to be with me!.

I didn't want to have to pretest to make sure nothing was something before I tested nothing against something :-). Hmm ... I don't think I could be more confusing if I tried.

  • 写回答

2条回答 默认 最新

  • duanche4578 2011-08-19 23:44
    关注

    There are actually several nothings in the string "test". They are (at a minimum, see my aside below):

    <nothing>t<nothing>e<nothing>s<nothing>t<nothing>
    

    That's why you're getting 1 as a result, it's finding that first nothing before the t.

    And I suspect that either you're misremembering that song or the bod who wrote it was not an engineer. Nothing from something leaves something (assumng we're talking subtractive operations here).

    If it was "something from nothing leaves nothing", that would make more sense but, being the anal-retentive personality type I am, I would have to insist that you can't get something from nothing :-)


    As an aside, I'd be interested in what preg_match_all returned for that. Some may think it's 5 (and I suspect it is) but some may argue, from a purely theoretical viewpoint, that there are an infinite number of nothings at each of those points in the string. I would hope the developers of PHP were more practical than that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!