与 NL2BR 类似的函数,但使用 <w:br /> 标记并删除任何中断线


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

http://sg2.php.net/manual/en/function.nl2br.php 有一个示例

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

这返回

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

我想要的是能够用这个<w:br />替换所有换行符。而且我不想再看到任何换行符。

换句话说,我想回来

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

我该如何实现此目的?

当我把

问题写到一半时,我想出了答案。

万一,其他人也有同样的问题。

/**
 *
 * 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);
    }

然后调用该函数

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