Php: truncate br tags from the beginning of a line?

I know that:

preg_replace('<br\s*\/?>', '', $string);

will remove all br tags from the string $ ...

How can we remove tags <br><br/><br />only if they are at the very beginning of the $ line? ($ string in my case there is html code with various tags ...)

+5
source share
3 answers

Just add the appropriate anchor ( ^):

preg_replace('/^(?:<br\s*\/?>\s*)+/', '', $string);

This will match several <br>at the beginning of the line.

(?:…) - , , . - (…) , , .

+16

PCRE . :

$string = preg_replace('/^\s*(?:<br\s*\/?>\s*)*/i', '', $string);

, .

:

  • ^\s*
  • (?:<br\s*\/?>\s*)* BR ( HTML, XHTML),
+4
$string = preg_replace( '@^(<br\\b[^>]*/?>)+@i', '', $string );

Should match:

<br>
<br/>
<br style="clear: both;" />
etc
+2
source

All Articles