php 将 <br /> 转换为新行以在文本区域中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6004343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:09:00  来源:igfitidea点击:

Converting <br /> into a new line for use in a text area

phphtml

提问by someguy

If I have a variable:

如果我有一个变量:

$var1 = "Line 1 info blah blah <br /> Line 2 info blah blah";

And a text area:

和一个文本区域:

<textarea>echo $var1</textarea>

How can I get the text area to display a new line instead of displaying the text on a single like with a <br />in it?

如何让文本区域显示一个新行,而不是在单个像中显示文本<br />

Edit:I have tried the following:

编辑:我尝试了以下方法:

<textarea class="hobbieTalk" id="hobbieTalk" name="hobbieTalk" cols="35" rows="5" onchange="contentHandler('userInterests',this.id,this.value,0)"><?php

$convert=$_SESSION["hobbieTalk"];
$convert = str_replace("<br />", "\n", $convert);
echo $convert;

?></textarea>

However the text area still contains the brtags in the lines.

但是文本区域仍然包含br行中的标签。

回答by Mobilpadde

Try this one

试试这个

<?php
    $text = "Hello <br /> Hello again <br> Hello again again <br/> Goodbye <BR>";
    $breaks = array("<br />","<br>","<br/>");  
    $text = str_ireplace($breaks, "\r\n", $text);  
?>  
<textarea><?php echo $text; ?></textarea>

回答by aftamat4ik

i am use following construction to convert back nl2br

我正在使用以下结构来转换回 nl2br

function br2nl( $input ) {
    return preg_replace('/<br\s?\/?>/ius', "\n", str_replace("\n","",str_replace("\r","", htmlspecialchars_decode($input))));
}

here i replaced \nand \rsymbols from $input because nl2br dosen't remove them and this causes wrong output with \n\nor \r<br>.

在这里,我替换了 $input 中的\n\r符号,因为 nl2br 不会删除它们,这会导致错误输出\n\n\r<br>

回答by Jonathan

The answer by @Mobilpadde is nice. But this is my solution with regex using preg_replacewhich might be faster according to my tests.

@Mobilpadde 的回答很好。但这是我使用preg_replace使用正则表达式的解决方案,根据我的测试,它可能会更快。

echo preg_replace('/<br\s?\/?>/i', "\r\n", "testing<br/><br /><BR><br>");

echo preg_replace('/<br\s?\/?>/i', "\r\n", "testing<br/><br /><BR><br>");

function function_one() {
    preg_replace('/<br\s?\/?>/i', "\r\n", "testing<br/><br /><BR><br>");
}

function function_two() {
    str_ireplace(['<br />','<br>','<br/>'], "\r\n", "testing<br/><br /><BR><br>");
}

function benchmark() {
    $count = 10000000;
    $before = microtime(true);

    for ($i=0 ; $i<$count; $i++) {
        function_one();
    }

    $after = microtime(true);
    echo ($after-$before)/$i . " sec/function one\n";



    $before = microtime(true);

    for ($i=0 ; $i<$count; $i++) {
        function_two();
    }

    $after = microtime(true);
    echo ($after-$before)/$i . " sec/function two\n";
}
benchmark();

Results:

结果:

1.1471637010574E-6 sec/function one (preg_replace)
1.6027762889862E-6 sec/function two (str_ireplace)

回答by Svetoslav Marinov

Here is another approach.

这是另一种方法。

class orbisius_custom_string {
    /**
     * The reverse of nl2br. Handles <br/> <br/> <br />
     * usage: orbisius_custom_string::br2nl('Your buffer goes here ...');
     * @param str $buff
     * @return str
     * @author Slavi Marinov | http://orbisius.com
     */
    public static function br2nl($buff = '') {
        $buff = preg_replace('#<br[/\s]*>#si', "\n", $buff);
        $buff = trim($buff);

        return $buff;
    }
}

回答by Crashspeeder

EDIT: previous answer was backwards of what you wanted. Use str_replace. replace <br>with \n

编辑:先前的答案与您想要的相反。使用 str_replace。替换<br>成\n

echo str_replace('<br>', "\n", $var1);

回答by Dan LaManna

<?php

$var1 = "Line 1 info blah blah <br /> Line 2 info blah blah";
$var1 = str_replace("<br />", "\n", $var1);

?>

<textarea><?php echo $var1; ?></textarea>