I have a text field retrieved by a Solr query that contains the body of an email. I am trying to replace embedded line breaks with paragraph tags via PHP like so:
$text = $item->Body[0]; $new_line = array("rn", "n", "r"); preg_replace($new_line, '</p><p>', $text); echo $text;
When I show the result in my IDE/debugger the newline characters are not replaced and are still there:
I have been going through threads on this site trying patterns suggested by different people including “/s+/” and PHP_EOL and “/(rn|r|n)/” and nothing works. What am I doing wrong?
Advertisement
Answer
You are missing the delimiter around your regex strings and you are not assigning the value.
You can also reduce your regex:
$text = preg_replace("/r?n|r/", '</p><p>', $text);
You might want switch to the multibyte safe version. They work with Unicode and you don’t need delimiters there 😉
$text = mb_ereg_replace("r?n|r", '</p><p>', $text);