I've been working on a catagory system which outputs all catagories within the database.
Because I am pulling infinite catagories (theoratically) I sometimes have <ul>
elements that do not contain <li>
elements (or anything else for that matter, except of whitespaces)
I am currently using jQuery to filter these <ul>
elements, but as you probaply agree that's not the most efficient way.
I've been trying to make a regex in order to replace these empty <ul>
elements with empty strings, but I haven't had much luck so far.
<ul class="nav navbar-nav catagory" style="display:none;">
</ul>
The HTML above is a example of a empty <ul>
I need to filter out.
I have this regex statement so far but it doesn't work as expected.
$str = '<ul class="nav navbar-nav catagory" style="display:none;"> </ul>';
preg_match('!\<ul\>/ +/\<\/ul\>!', $str, $matches);
Can anyone help me out?
Also the content of the catagories is stored in a variable inside PHP.
EDIT:
I solved this issue thanks to Josh Beam, I editted my function in which the catagories were bieing pulled from teh database:
function build_catagory($parent, $row = NULL)
{
global $db, $template;
// Initialise array
$data = array();
// Next level parent
$next = $parent + 1;
// Basic SQL statement
$sql = "SELECT * FROM Rubriek";
// Where condition based on $row
if(is_null($row))
{
$where = " WHERE Hoofdrubriek IS NULL";
}
else
{
$where = " WHERE Hoofdrubriek = '" . $row['Rubrieknummer'] . "'";
}
// Execute query
$stmt = $db->query($sql . $where);
if($stmt)
{
// Create new instance of template engine (set output to false)
$catagory = new Template('{', '}', array('content'), FALSE);
// Load template
$catagory->load(T_TEMPLATE_PATH . '/rubrieken.html', 'content');
// Fetch results
while($row = $db->fetch($stmt))
{
$data[] = array(
'CATAGORY_NAME' => ucfirst(strtolower($row['Rubrieknaam'])),
'CATAGORY' => build_catagory($next, $row),
);
}
// Assign data to the template
$catagory->assign_vars(array(
'CATAGORIES' => $data,
'CATAGORIES_DISPLAY' => ($parent == 0 ? '' : 'style="display:none;"'),
));
// Return catagory
return $catagory->parse();
}
else
{
return '';
}
}
The fix was quite easy actually, just resturn a empty string instead of a parse from the template. Thanks for the help!