replace br tag from a string in php

后端 未结 5 1153
抹茶落季
抹茶落季 2021-01-02 07:39

I have the following string. I want to replace the line break with /n

Good FRIENDS are hard to find,
harder to leave,
相关标签:
5条回答
  • 2021-01-02 08:07
    preg_replace("/<br\W*?\/>/", "\n", $your_string);
    
    0 讨论(0)
  • 2021-01-02 08:07

    Have you tried str_replace?

    str_replace("<br />", "\n", $your_string);
    
    0 讨论(0)
  • 2021-01-02 08:08

    You can also try below regex:

    $string = preg_replace( '@^(<br\\b[^>]*/?>)+@i', '', $string );
    
    0 讨论(0)
  • 2021-01-02 08:13

    Use This function

       function separate( $str,$subStr, $count )
      {
         $formatStr = '';
         $start=0;
         $num = 1;
         while(!(strpos($str,$subStr,$start) === null))
         { 
           $first = strpos($str,$subStr,$start);
           if ($first < $start)
              break; 
           $newStr = substr($str,$start,$first - $start + 1 );
           $formatStr .= $newStr;
           if ($num % $count == 0)
              $formatStr .= '<br>';
           $num ++;
           $start = $first +1;
         }
         return $formatStr;
      }
    

    Example

    $str = 'AAA.BBB.CCC.DDD.EEE.FFF.CCC';
    echo separate ($str,'.', 3);
    

    Output

    AAA.BBB.CCC.
    DDD.EEE.FFF.
    
    0 讨论(0)
  • 2021-01-02 08:17

    Use str_replace

    $text = str_replace("<br />", "\n", $text);
    

    If there is actually a line break within the <br /> tag as in your sample code, try this:

    $text = preg_replace("/<br\n\W*\/>/", "\n", $text);
    
    0 讨论(0)
提交回复
热议问题