2015-09-25 27 views
0

我得到這個:如何獲得多行匹配以在NOWDOC字符串上工作?

$p= <<<'EOD' 
stuff 
more 
EOD; 
    print (preg_match ('~^stuff$~m', $p)); // expected 1, got 0 

儘管

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

的「行首」和「行結束」構建匹配立即 以下或立即在受任何換行符前字符串,分別爲

編輯:這是在一個標準的Windows文本文件。

+0

您應該使用'/'而不是'〜'作爲模式的開始和結束。 –

回答

1

您可能使用CRLF或CR而不是換行換行。切換到僅使用LF或normalize your string to it,然後再運行preg_match並得到結果just work.

+0

謝謝。這適用於Windows:print(preg_match('〜^ stuff $〜m',str_replace(「\ x0d \ x0A」,「\ x0A」,$ p)));儘管我希望爲所有平臺提供解決方案......並且奇怪地規範PHP_EOL失敗。 – ChrisJJ

+0

順便說一句,你的「只是工作」有一個語法錯誤。 – ChrisJJ

+0

這是一個語法錯誤,然後:) – ChrisJJ

相關問題