【问题标题】:How can I get multiline match to work on NOWDOC strings?如何获得多行匹配以处理 NOWDOC 字符串?
【发布时间】:2015-09-26 01:34:52
【问题描述】:

我明白了:

  $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 文本文件中。

【问题讨论】:

  • 你应该使用'/'而不是'~'作为模式的开始和结束。

标签: php


【解决方案1】:

您可能会使用 CRLF 或 CR 而不仅仅是换行符来换行。在运行 preg_match 之前切换到仅使用 LF 或 normalize your string to it,您将获得 just work.

【讨论】:

  • 谢谢。这适用于 Windows: print (preg_match ('~^stuff$~m', str_replace("\x0d\x0A","\x0A",$p)));虽然我想要一个适用于所有平台的解决方案......但奇怪的是对 PHP_EOL 的规范化失败了。
猜你喜欢
  • 1970-01-01
  • 2015-12-22
  • 2014-03-26
  • 2019-10-17
  • 2020-07-31
  • 1970-01-01
  • 2022-12-04
  • 2022-01-14
  • 1970-01-01
相关资源
最近更新 更多