Kim Stacks
Kim Stacks

Reputation: 10812

a similar function to nl2br but using <w:br /> tags and removing any break lines

https://www.php.net/manual/en/function.nl2br.php has an example

<?php
$string = "This\r\nis\n\ra\nstring\r";
echo nl2br($string);
?>

This returns

This<br />
is<br />
a<br />
string<br />

What I want is to be able to replace all the newlines with this <w:br />. And I do not want to see any more newlines.

In other words, I want to get back

This<w:br />is<w:br />a<w:br />string<w:br />

How do I accomplish this?

Upvotes: 2

Views: 545

Answers (1)

Kim Stacks
Kim Stacks

Reputation: 10812

As I was writing out the question halfway, I figured out the answer.

In case, somebody else has the same question.

/**
 *
 * Replace newline
 *
 * Replace newlines with something else
 *
 * @param $subject String The subject we are searching for newlines and replace
 * @param $replace String The replacement for the newlines
 * @return String The new subject with the newlines replaced
 */     
    function replaceNewLines($subject, $replace) {
        return str_replace(array("\r\n", "\n\r", "\n", "\r"), $replace, $subject);
    }

Then you call the function

$replacedContent = replaceNewLines($content, "<w:br />");

Upvotes: 2

Related Questions