Marcus
Marcus

Reputation: 295

How to convert more than two <br/> tag to a double <br/><br/> in php

Do not convert

<br/>
<br/><br/>

Convert, if there are more than two newline tag

<br/><br/><br/>

into

<br/><br/>

How to add my condition to this?

preg_replace("/(<br\s*\/?>\s*)+/", "<br/><br/>", $input);

Upvotes: 0

Views: 421

Answers (3)

Hebrus
Hebrus

Reputation: 133

Using the metacharacter {min,max} after the regex will help you.

The wikipedia actually does a really good job, also the regex.info is really helpful.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

I would recommend you to use \h instead of space where \h matches all type of horizontal spaces.

preg_replace('~(<br\h*\/?>){2,}~', '<br/><br/>', $string);

Upvotes: 0

vks
vks

Reputation: 67968

(<br[ ]*\/?>){2,}

Simply use this.And replace it by <br/><br/>.

Upvotes: 3

Related Questions