Doug Cassidy
Doug Cassidy

Reputation: 1907

Strip single ended tags from string (img, hr, etc)

function stripSingleEndedTag($content, $allowed = array()){
(array)$allowed;
$singletags = array('<meta>','<img>','<input>','<hr>','<br>','<link>','<isindex>','<base>','<meta>','<nextid>','<bork>');

$stripthese = arrayfilterout ($singletags, $allowed);
$stripthese = str_replace(array('<','>'),'',$stripthese);

$pattern = '/<('. implode('|', $stripthese) .')[^>]+\>/i'; 

$content = preg_replace($pattern, "", $content);
return $content;
}

What I've got here will strip out a single ended tag like <bork /> but only if there is some character after 'bork' and before '>'. <bork > and <bork/> are stripped, but not <bork>

BTW, cant use strip_tags().

Upvotes: 0

Views: 233

Answers (1)

OregonTrail
OregonTrail

Reputation: 9019

You can use:

$pattern = '/\<('. implode('|', $stripthese) .')[^>]*\>/i';

Original Answer:

Do you want to get rid of <bork /> and <bork/>, but not <bork> and <bork >?

It looks like what you want is:

$pattern = '/<('. implode('|', $stripthese) .').*\\\>/i';

Update: fix for greedy match:

$pattern = '/<('. implode('|', $stripthese) .').*?\\\>/i';
$pattern = '/<('. implode('|', $stripthese) .')[^>]*\\\>/i';

Upvotes: 2

Related Questions