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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -