I have the following string. I want to replace the line break with /n
Good FRIENDS are hard to find,
harder to leave,
preg_replace("/<br\W*?\/>/", "\n", $your_string);
Have you tried str_replace?
str_replace("<br />", "\n", $your_string);
You can also try below regex:
$string = preg_replace( '@^(<br\\b[^>]*/?>)+@i', '', $string );
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.
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);