duanqilupinf67040 2012-12-24 09:39
浏览 11

preg_match随机行为

I have, among others, these strings: "boards/", "localhost", "mysql", "0", "1", "Testing Board", "PLAIN".

I receive them as post data from a form. Some of these can have multibyte chars, but none of the test strings used has multibyte chars.

I run them through if (!preg_match('/^./u', $str)).

That if check mostly evaluates to false, ie the preg_match returns 1. But sometimes it evaluates to true, ie 0 from that preg_match.

The true (0) seems to come totally random. But if one of the strings evaluates to true then they all do. It's either all or none.

Do anybody have some sort of explanation to that seemingly random behavior? And maybe a solution to fix it?

Cheers Jari

  • 写回答

1条回答 默认 最新

  • dongqian3198 2012-12-24 09:46
    关注

    Have a look at the warning in the docs. http://php.net/manual/en/function.preg-match.php

    Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

    When you are running the preg_match on values as 0 and 1 it will return that match. Which will cause an evaluation to true when not handled correctly.

    1 == true   //true
    1 === true  //false
    0 == false  //true
    0 === false //false
    1 == false  //false
    1 === false //false
    0 == true   //false
    0 === true  //false
    
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了