Jump to content


Photo

To remove empty HTML tags


  • Please log in to reply
1 reply to this topic

#1 groover

groover
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 16 September 2006 - 11:29 AM

Hi everyone.

I would like to ask, how can I remove all the empty HTML tags from a user input?

I want to remove things like the following:

<b>&nbsp;</b>
<p align=center>\n&nbsp;\n</p>
<div><br>\n&nbsp;<i></i></div>

How can I do it?

Thanks first.

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 18 September 2006 - 07:28 PM

Try this:

<pre>
<?php

	$tests = array(
		### Empty.
		'<b>&nbsp;</b>',
		"<p align=center>\n&nbsp;\n</p>",
		"<div><br>\n&nbsp;<i></i></div>",
		'<font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>',
		'<body></body>',
		### Not Empty.
		'<div>content</div>',
		"<span>line\nbreak</span>",
		'<b>a</b>',
	);
	
	function remove_empties ($matches) {
		### Variableize the stuff between the tags.
		$content = $matches[2];
		### Remove all nbsps, empty tags, brs, and whitespace.
		$content = str_replace('&nbsp;', '', $content);
		$content = preg_replace('%<(\w+)[^>]*></\1>%', '', $content);
		$content = preg_replace('%<br/?>%', '', $content);
		$content = preg_replace('/\s/s', '', $content);
		### If there is still content the tag innards are not empty,
		### send back the original match. Otherwise, send empty.
		return $content ? $matches[0] : '' ;
	}
	
	echo '<table border="1">';
	foreach ($tests as $test) {
		echo '<tr><td>', htmlentities($test), '</td>';
		$test = preg_replace_callback('%
			<(\w+)[^>]*>
			(.*?)
			</\1>
		%xs', 'remove_empties', $test);
		echo '<td>', htmlentities($test), '</td></tr>';
	}
	echo '</table>';
?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users