dongxu1875 2012-04-01 08:52
浏览 12
已采纳

检查字符串中的新行

Can anyone tell me why this isn't working? It always returns false.

$str = "huuhhu

moo.com
www";

if (preg_match('/(\
|\\
|\)/', $str) === true) {
    echo "True";
} else {
    echo "False";
}
  • 写回答

2条回答 默认 最新

  • douyun4524 2012-04-01 08:56
    关注

    preg_match doesn't return true. It returns the number of matches. You need to do this:

    $str = "huuhhu
    
    moo.com
    www";
    
    if (preg_match('/(\
    |\\
    |\)/', $str)) {
        echo "True";
    } else {
        echo "False";
    }
    

    Also, you could probably simplify your expression to this:

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

报告相同问题?