使用regex,替换文本块中以“”开头的任何一行;嵌入:“;


Using regex, replace any line within a block of text that starts with "embed:"

正如标题所说,我正在寻找一个使用php代码的正则表达式,它给出了一个带换行符的$字符串,例如:

Hello my name is John Doe. Here is a cool video:
embed:http://youtube.com/watch......
I hope you liked it!

它会返回:

Hello my name is John Doe. Here is a cool video:
I hope you liked it!

这应该做到:

preg_replace('/^embed:.*'s*/m', '', $block_of_text);

说明:

  1. /m修改器启用了多行模式(因此您可以轻松匹配基于行的模式)

  2. 它使用插入符号(锚点)匹配行的开头:^

  3. 匹配"embed:字符串

  4. 使用.* 匹配到行的末尾

  5. 匹配当前行之后的任何换行符和空格(这样可以更好地清理空行)

试试这个:

preg_replace('#embed:.*?'n*#m', '', $string);