我有这样的文本:
some text 'r'n 'r'n'r'n'r'n'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n some text 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n some text 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n 'r'n
我需要用一个<br/>
替换多个'r'n
的每个块
我试着使用str_replace('''r''n','<br/>',$text);
,但最终使用了太多<br/>
我需要最终的输出是这样的:
some text <br/> some text <br/> some text <br/>
使用带有非捕获组和量词的正则表达式:
$result = preg_replace('/(?:'r'n *)+/', '<br />', $subject);
解释:
(?: # Start a group which matches:
'r'n # one newline combination
[ ]* # followed by zero or more spaces
)+ # Repeat the entire group once or more, as many times as possible
使用正则表达式:
$output = preg_replace(',('r'n)+,', '<br />', $input);