Converting
into a new line for use in a text area

前端 未结 6 1189
悲&欢浪女
悲&欢浪女 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('//i', "\r\n", "testing



    ");

    function function_one() {
        preg_replace('//i', "\r\n", "testing



    "); } function function_two() { str_ireplace(['
    ','
    ','
    '], "\r\n", "testing



    "); } 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)
    

提交回复
热议问题