Converting
into a new line for use in a text area

前端 未结 6 1176
悲&欢浪女
悲&欢浪女 2020-11-28 09:23

If I have a variable:

$var1 = \"Line 1 info blah blah 
Line 2 info blah blah\";

And a text area:



        
相关标签:
6条回答
  • 2020-11-28 09:32

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

    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)
    
    0 讨论(0)
  • 2020-11-28 09:32

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 09:40

    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>
    
    0 讨论(0)
  • 2020-11-28 09:40
    <?php
    
    $var1 = "Line 1 info blah blah <br /> Line 2 info blah blah";
    $var1 = str_replace("<br />", "\n", $var1);
    
    ?>
    
    <textarea><?php echo $var1; ?></textarea>
    
    0 讨论(0)
  • 2020-11-28 09:48

    i am use following construction to convert back nl2br

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

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

    0 讨论(0)
  • 2020-11-28 09:53

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

    echo str_replace('<br>', "\n", $var1);
    
    0 讨论(0)
提交回复
热议问题