datasn.io
datasn.io

Reputation: 12867

Regular expression to match strings that contain <br/> but not other HTML tags?

I want to find and match strings that contain no HTML tags but <br/> and all other normal characters ([^<>]+).

So basically, this match dismisses any string that contains '<' or '>' but not '<br/>'.

This is what I can come up with:

preg_match('@[(?:<br/>).]+@sU', $str, $match);

Obviously it doesn't work cause' I don't know what to put at the dot. Any ideas?

Upvotes: 0

Views: 825

Answers (2)

VoteyDisciple
VoteyDisciple

Reputation: 37803

Why not...

@(?:[^<]|<br */>)*@

That is, any number of (a complete <br /> tag or any non-tag-opening character).

Upvotes: 1

Ben Lee
Ben Lee

Reputation: 53319

I would just do it backwards -- see if the string contains any <...> tag other than <br/> and dismiss it if it does. So:

preg_match(/<(?!br)/i, $str, $match);
if (!$match) we_are_good();

Upvotes: 1

Related Questions