Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
正如标题所说,我正在寻找一个使用 php 代码的正则表达式,它给出了一个带有换行符的 $string,如下所示:
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:.*?\n*#m', '', $string);
这应该这样做:
preg_replace('/^embed:.*\s*/m', '', $block_of_text);
解释:
/m修饰符启用多行模式(因此您可以轻松匹配基于行的模式)
/m
它使用插入符号(锚)匹配行的开头:^
^
匹配"embed:字符串
"embed:
使用匹配直到行尾.*
.*
匹配当前行之后的任何换行符和空格(这可以更好地清理空行)