正则表达式检查最后一段中的字符串

时间:2013-07-12 12:26:24

标签: php regex

我有一个像

这样的文字
Hello this is test 
Text:12563

Test This is also Test.This is test

现在这里文字:12563不在最后一段,但如果我有像

这样的文字
Hello this is test     

Test This is also Test.
Text:12563
This is test

这里的文字:12563是给定文本的最后一段。现在我必须使用preg_match_all函数检查它。你有没有想过这个正则表达式。

2 个答案:

答案 0 :(得分:1)

检查有2个后续换行符(新章节)的位置,并在上次遇到2个后续换行符后检查包含文本的文本结束前的多行文字。

答案 1 :(得分:0)

这是一个不使用正则表达式的答案。

$text = <<<EOF
Hello this is test     

Test This is also Test.
Text:12563
This is test
EOF;

$paras = explode("\n\n", $text);

$last = $paras[(count($paras) - 1)];

$needle = "Text:12563";

if (strpos($last, $needle)) {
    echo "Found";
}