2

我有这样的文字:

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

我需要用一个替换每个多\r\n<br/>

我尝试使用str_replace('\\r\\n','<br/>',$text);,但结果太多了<br/>

我需要最终输出是这样的:

some text <br/> some text <br/> some text <br/>

4

2 回答 2

6

Use a regex with a non-capturing group and quantifiers:

$result = preg_replace('/(?:\r\n *)+/', '<br />', $subject);

Explanation:

(?:   # 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
于 2013-11-10T12:43:23.627 回答
2

使用正则表达式

$output = preg_replace(',(\r\n)+,', '<br />', $input);
于 2013-11-10T12:43:24.300 回答