Hello Guys,
Here i have wrote the new function and REGEX pattern to remove the nested empty tags (nested) from string. Watch yourself below.
<?php
/**
* remove_empty_tags_recursive ()
* Remove the nested HTML empty tags from the string.
*
* @author Junaid Atari <mj.atari@gmail.com>
* @version 1.0
* @param string $str String to remove tags.
* @param string $repto Replace empty string with.
* @return string Cleaned string.
*/
function remove_empty_tags_recursive ($str, $repto = NULL)
{
//** Return if string not given or empty.
if (!is_string ($str)
|| trim ($str) == '')
return $str;
//** Recursive empty HTML tags.
return preg_replace (
//** Pattern written by Junaid Atari.
'/<([^<\/>]*)>([\s]*?|(?R))<\/\1>/imsU',
//** Replace with nothing if string empty.
!is_string ($repto) ? '' : $repto,
//** Source string
$str
);
}
/*
+=====================================
| EXAMPLE
+=====================================
*/
$str=<<<EOF
<p></p>
<div id="paralax">
<div><p></p>Hello User,<strong></strong></div>
<div id="contents">Welcome to our domain.</div><p><p><p></p></p></p>
</div>
EOF;
echo remove_empty_tags_recursive ($str);
/*
+=====================================
| OUTPUT:
+=====================================
*/
/*<div id="paralax">
<div>Hello User,</div>
<div id="contents">Welcome to our domain.</div>
</div>*/
Hope you like it.
Here i have wrote the new function and REGEX pattern to remove the nested empty tags (nested) from string. Watch yourself below.
<?php
/**
* remove_empty_tags_recursive ()
* Remove the nested HTML empty tags from the string.
*
* @author Junaid Atari <mj.atari@gmail.com>
* @version 1.0
* @param string $str String to remove tags.
* @param string $repto Replace empty string with.
* @return string Cleaned string.
*/
function remove_empty_tags_recursive ($str, $repto = NULL)
{
//** Return if string not given or empty.
if (!is_string ($str)
|| trim ($str) == '')
return $str;
//** Recursive empty HTML tags.
return preg_replace (
//** Pattern written by Junaid Atari.
'/<([^<\/>]*)>([\s]*?|(?R))<\/\1>/imsU',
//** Replace with nothing if string empty.
!is_string ($repto) ? '' : $repto,
//** Source string
$str
);
}
/*
+=====================================
| EXAMPLE
+=====================================
*/
$str=<<<EOF
<p></p>
<div id="paralax">
<div><p></p>Hello User,<strong></strong></div>
<div id="contents">Welcome to our domain.</div><p><p><p></p></p></p>
</div>
EOF;
echo remove_empty_tags_recursive ($str);
/*
+=====================================
| OUTPUT:
+=====================================
*/
/*<div id="paralax">
<div>Hello User,</div>
<div id="contents">Welcome to our domain.</div>
</div>*/
Hope you like it.
2 comments:
Nice! I tried this, but I could not get empty <a href="..."></a> to go away. Nice looking work though.
It's just remove the empty tags have no data, also will skip attributes.
Post a Comment