PHP preg_match和多行直到空行

时间:2012-10-27 13:03:07

标签: php preg-match string

我想通过php preg_march从电子邮件中获取文本,我的文字如下:

Details: Cras ultrices quam id nisl

Additional Informations
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nunc eu enim non ipsum feugiat aliquam. Cras eget orci tellus, ac feugiat ligula.
Proin tristique lobortis lacinia. Morbi facilisis

Next line with value: Lorem ipsum dolor sit

我希望在“空白行”之前得到“附加信息”以下的内容。

1 个答案:

答案 0 :(得分:0)

'~Additional Informations\\r?\\n(.+?)\\r?\\n\\r?\\n~si'

试试这个。

确实有效:

$string = <<<SZ
Details: Cras ultrices quam id nisl

Additional Informations
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nunc eu enim non ipsum feugiat aliquam. Cras eget orci tellus, ac feugiat ligula.
Proin tristique lobortis lacinia. Morbi facilisis

Next line with value: Lorem ipsum dolor sit
SZ;
preg_match('~Additional Informations\\r?\\n(.+?)\\r?\\n\\r?\\n~si', $string, $matches);
var_dump($matches[1]);