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条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示