Reputation: 111
I'm having text like this:
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
I need to replace each block of multiple \r\n
with just one <br/>
I tried to use str_replace('\\r\\n','<br/>',$text);
but I ended up with too many <br/>
I need the final output to be like this:
some text <br/> some text <br/> some text <br/>
Upvotes: 2
Views: 498
Reputation: 37984
Use regular expressions:
$output = preg_replace(',(\r\n)+,', '<br />', $input);
Upvotes: 2
Reputation: 336128
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
Upvotes: 6