regex - PHP - Remove duplicate HTML tags -
this question has answer here:
i have php string unknown number of br
tags. need keep one.
start string
$str = 'a line of string data<br><br><br><br>with duplicate br tags in here.'; $str2 = 'another line 5 br tags time.<br><br><br><br><br>new line.'; $str3 = 'when it<br /><br><br />breaks';
result
$str = 'a line of string data<br>with duplicate br tags in here.'; $str2 = 'another line 5 br tags time.<br>new line.'; $str3 = 'when it<br>breaks';
my thoughts
- first used
str_replace('<br>', '', $str)
. don't go because number of duplicate tags in row unknown. - some clever regex might solve it? or solution?
- would nice if work or without ending slash.
<br><br />
try this..
$str = 'a line of string data<br><br><br><br>with duplicate br tags in here.'; echo preg_replace('#(<br\s?/?>)+#', '<br>', $str);
output
a line of string data<br>with duplicate br tags in here.
Comments
Post a Comment