Vadim
Vadim

Reputation: 27

Remove text between tags using a regular expression

How to remove text between br tags , using regexp

<br />var j=; var g=; g+=G;j+=30006;g+=a;g+=u;g+=l;g+=t;<br />

"var" and ";" is always exist, but other words and symbols may vary, something like this:

<br />var e=; var x=; x+=tos;x+=ах,;<br />

etc...

these lines appear in random order:

<br />Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br />var j=; var g=; g+=G;j+=30006;g+=a;g+=u;g+=l;g+=t;<br /> Donec condimentum neque quis magna consectetur ut aliquam quam dictum. Fusce sed elit purus, lobortis tincidunt libero.<br />Nunc ultrices augue non augue tristique in aliquet urna adipiscing.<br /><br />var ... ;<br />

thanks!

Upvotes: 1

Views: 1153

Answers (4)

MPękalski
MPękalski

Reputation: 7103

The regexp you want would be

<br\s\/?>.*?var.*?;.*?<br\s\/?>

and just replace the output by <br /><br />.

Upvotes: 1

Arkh
Arkh

Reputation: 8459

If you have to do it for a whole text and by pairs of <br/> you can do something like that :

$str = 'test0 <br />var j=; var g=; g+=G;j+=30006;g+=a;g+=u;g+=l;g+=t;<br /> test1 <br /> test2';
$result = preg_replace('`<br */>.*?<br */>`s','<br/><br/>',$str);
var_dump($result);

Upvotes: 0

Headshota
Headshota

Reputation: 21449

if count of <br /> elements vary you can use substr_count() to count <br /> elements and then output same amount separately.

Upvotes: 0

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234384

If all you want is the output <br /><br />, you don't need regular expressions:

$result = "<br /><br />";

It's just a constant!

Upvotes: 0

Related Questions