开发者

Regex for use with PHP's preg_match() to find a newline

I'm fairly new/rusty with regular expressions. I'm collecting a block of text from a textarea element and I want to check to see if the person who filled it used any par开发者_JAVA技巧agraphs, amongst other things.

I'm using the following and I know it's wrong.

preg_match('/\r\n|\n|\r/', $_GET['text']);


Your regex is not wrong. But for detecting paragraphs you will want to look for two consecutive newlines:

 preg_match('/(\r?\n){2}/'

The carriage return \r is optional, and I would just check for \n newline as most platforms treat that as linebreak. Obviously this check will fail if the submitted text is just a single line without paragraphs or newlines.

Alternatively you could also probe for two newlines with any sort of whitespace in between:

 preg_match('/(\s*\n){2}/'


Assuming:

This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.

And this is another. And this is another. And this is another. And this is another. And this is another. And this is another. And this is another. And this is another. And this is another. And this is another.

You could just do:

if (str_replace("\r\n\r\n", '', $str) != $str)
  // the input contains at least two paragraphs
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜