新行到段落的功能


New line to paragraph function

我有这个有趣的函数,我用它在段落中创建新的行。我使用它而不是nl2br()函数,因为它输出格式更好的文本。

function nl2p($string, $line_breaks = true, $xml = true) {
$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);
// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($line_breaks == true)
    return '<p>'.preg_replace(array("/(['n]{2,})/i", "/([^>])'n([^<])/i"), array("</p>'n<p>", '<br'.($xml == true ? ' /' : '').'>'), trim($string)).'</p>';
else 
    return '<p>'.preg_replace(
    array("/(['n]{2,})/i", "/(['r'n]{3,})/i","/([^>])'n([^<])/i"),
    array("</p>'n<p>", "</p>'n<p>", '<br'.($xml == true ? ' /' : '').'>'),
    trim($string)).'</p>'; 

}

问题是,每当我尝试创建单个换行符时,它都会无意中删除其下一段的第一个字符。我对正则表达式不太熟悉,无法理解是什么导致了这个问题。

这里是另一种不使用正则表达式的方法。注意,此函数将删除所有单行换行符。

function nl2p($string)
{
    $paragraphs = '';
    foreach (explode("'n", $string) as $line) {
        if (trim($line)) {
            $paragraphs .= '<p>' . $line . '</p>';
        }
    }
    return $paragraphs;
}

如果你只需要在你的应用程序中这样做一次,并且不想创建一个函数,它可以很容易地内联完成:

<?php foreach (explode("'n", $string) as $line): ?>
    <?php if (trim($line)): ?>
        <p><?=$line?></p>
    <?php endif ?>
<?php endforeach ?>

问题在于您对单行换行符的匹配。它匹配换行前的最后一个字符和换行后的第一个字符。然后你用<br>替换匹配,所以你也失去了那些字符。你需要把它们放在替代品中。

试试这个:

function nl2p($string, $line_breaks = true, $xml = true) {
$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);
// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($line_breaks == true)
    return '<p>'.preg_replace(array("/(['n]{2,})/i", "/([^>])'n([^<])/i"), array("</p>'n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'), trim($string)).'</p>';
else 
    return '<p>'.preg_replace(
    array("/(['n]{2,})/i", "/(['r'n]{3,})/i","/([^>])'n([^<])/i"),
    array("</p>'n<p>", "</p>'n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'),
    trim($string)).'</p>'; 
}

我也写了一个非常简单的版本:

function nl2p($text)
{
    return '<p>' . str_replace([''r'n', ''r', ''n'], '</p><p>', $text) . '</p>';
}

@Laurent的回答不适合我- else语句正在做$line_breaks == true语句应该做的事情,并且它正在将多个换行符转换为<br>标记,这是PHP的原生nl2br()已经做到的。

这是我设法得到的工作与预期的行为:

function nl2p( $string, $line_breaks = true, $xml = true ) {
    // Remove current tags to avoid double-wrapping.
    $string = str_replace( array( '<p>', '</p>', '<br>', '<br />' ), '', $string );
    // Default: Use <br> for single line breaks, <p> for multiple line breaks.
    if ( $line_breaks == true ) {
        $string = '<p>' . preg_replace(
            array( "/(['n]{2,})/i", "/(['r'n]{3,})/i", "/([^>])'n([^<])/i" ),
            array( "</p>'n<p>", "</p>'n<p>", '$1<br' . ( $xml == true ? ' /' : '' ) . '>$2' ),
            trim( $string ) ) . '</p>';
    // Use <p> for all line breaks if $line_breaks is set to false.
    } else {
        $string = '<p>' . preg_replace(
            array( "/(['n]{1,})/i", "/(['r]{1,})/i" ),
            "</p>'n<p>",
            trim( $string ) ) . '</p>';
    }
    // Remove empty paragraph tags.
    $string = str_replace( '<p></p>', '', $string );
    // Return string.
    return $string;
}

这是一种反向方法,可以将段落替换回常规换行符,反之亦然。

在构建表单输入时非常有用。在保存用户输入时,您可能希望将换行符转换为段落标记,但是在编辑表单中的文本时,您可能不希望用户看到任何html字符。然后我们将段落替换回换行符。

// This function will convert newlines to HTML paragraphs
// without paying attention to HTML tags. Feed it a raw string and it will
// simply return that string sectioned into HTML paragraphs
function nl2p($str) {
    $arr=explode("'n",$str);
    $out='';
    for($i=0;$i<count($arr);$i++) {
        if(strlen(trim($arr[$i]))>0)
            $out.='<p>'.trim($arr[$i]).'</p>';
    }
    return $out;
}
// Return paragraph tags back to line breaks
function p2nl($str)
{
    $str = preg_replace("/<p[^>]*?>/", "", $str);
    $str = str_replace("</p>", "'r'n", $str);
    return $str;
}

扩展@NaturalBornCamper的解决方案:

function nl2p( $text, $class = '' ) {
    $string = str_replace( array( "'r'n'r'n", "'n'n" ), '</p><p>', $text);
    $string = str_replace( array( "'r'n", "'n" ), '<br />', $string);
    return '<p' . ( $class ? ' class="' . $class . '"' : '' ) . '>' . $string . '</p>';
}

通过将双换行符转换为段落来处理它们,通过将它们转换为<br />

来处理单行换行符。

我用close代替双换行符,然后打开新的段落</p><p>。剩余的单行换行符转换为<br>标记。

$paragraphs = str_replace ("'r'n'r'n", "</p><p>", $text);
$noLines = str_replace ("'r'n", "<br>", $paragraphs );

如果你混合了HTML和非HTML内容,你可以在渲染时检查p标签是否存在:

if (str_contains ($blogBody, '<p>')) {
    echo $noLines;
} else {
    echo '<p>'.$noLines.'</p>';
}

在你的行间输入这个:

echo '<br>';

这会给你一个新的行