PHP Regex: How to match \r and \n without using [\r\n]?

后端 未结 7 1116
予麋鹿
予麋鹿 2020-11-27 05:58

I have tested \\v (vertical white space) for matching \\r\\n and their combinations, but I found out that \\v does not match \\r

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 06:25

    To match every LINE of a given String, simple use the ^$ Anchors and advice your regex engine to operate in multi-line mode. Then ^$ will match the start and end of each line, instead of the whole strings start and end.

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

    in PHP, that would be the m modifier after the pattern. /^(.*?)$/m will simple match each line, seperated by any vertical space inside the given string.

    Btw: For line-Splitting, you could also use split() and the PHP_EOL constant:

    $lines = explode(PHP_EOL, $string);
    

提交回复
热议问题