preg_match_all rtmp 链接及其标题

preg_match_all rtmp links and its titles

大家好,我有这个字符串:

All text has PHP_EOL at end of its line.
Some TITLE OF RTMP STREAM
rtmp://slww/best
Some simple text what not required to find
Another not required string line

使用此正则表达式获取 rtmp 或 http 链接,但我如何找到它的标题?

preg_match_all("/(rtmp:+\S*)|(http:+\S*)/s", $input_lines, $output_array);

rtmp 或 html 正则表达式工作正常,但我也需要捕获 rtmp 链接的标题。

已尝试添加 ^.*$^ 后跟我的 rtmp http 模式,因为 ^ 它的行首和 $ 它的结尾。 /s 选项用于搜索包括换行符在内的多行。

/^.*$^(rtmp:+\S*)|(http:+\S*)/s

但不知道如何找到它的标题。 简单的我试图存档的是让整行高于我的正则表达式或低于我的正则表达式。

在某些情况下,我需要抓住下面的整行。

有什么想法吗?

你实际上可以使用这个正则表达式:

(?m)^(?<TITLE>.*)\R+(?<url>(?:rtmp|http):\S*)

demo

请注意,您正在使用 /s 强制 . 匹配换行符的单行修饰符,但您没有在模式中使用点。您正在寻找使 ^ 在行首匹配的多行 /m 修饰符(我使用了内联版本 (?m))。

我没有使用单行修饰符,因此 .* 将只匹配 URL 行之前的行。我的正则表达式中的 \R 匹配换行符序列。