Forum Moderators: coopster

Message Too Old, No Replies

Remove empty tags

         

username

3:58 am on Mar 27, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all, am looking to remove tags which have no inner content i.e:

<a class="test"></a> - this would be removed
<a class="test">content</a> - this would not be removed

I have been playing with regular expressions attempting to do this with no luck thus far. Below is my effort:

$content = "<a></a><b>non-empty</b>";
$pattern = "/<[^\/>]*>([\s]?)*<\/[^>]*>/";
echo preg_replace($pattern, '', $content);

If anyone could assist, it would be appreciated.

Thanks.

username

11:51 pm on Mar 27, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



So just an update. Does anyone have any ideas about my topic?

coopster

12:46 am on Mar 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think you are going to need to apply a lookahead or lookbehind assertion here. The PCRE Pattern Syntax [php.net] page explains the application.

coopster

1:21 am on Mar 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, upon afterthought, if you are looking for empty tags you may not need to use assertion. Build your pattern to look for an <a> element with no characters before it's end tag.

coopster

1:30 am on Mar 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Something to get you started ...
$subject = '<a class="test"></a><p><- this would be removed</p> 
<p></p>
<a class="test">content</a>
';
$pattern = '/(<a[^>]*><\/a>)/i';
print htmlentities($subject). "<br />\n";
print htmlentities(preg_replace($pattern, '', $subject));
exit;

username

3:06 am on Mar 28, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks coopster. Got it working.