<?php
function wb_build_select_query($q) {
	$r = "select ";
	
	if(count($q['fields']) > 0) $r .= implode(", ", $q['fields']);
	else $r .= " * ";
	
	$r .= " from ";
	
	if(count($q['from']) > 0) {
		$r .= implode(", ", $q['from']);
	}
	
	$r .= " where ";
	
	$have_where = false;
	
	if(count($q['where']) > 0) {
		foreach ($q['where'] as $op=>$op_data) {
			if($op == "and") {
				if(count($op_data) > 0) {
					$r .= implode(" and ", $op_data);
					$have_where = true;
				}
			}
			
			if($op == "or") {
				if(count($op_data) > 0) {
					$r .= implode(" or ", $op_data);
					$have_where = true;
				}
			}
			
			if($op == "append") {
				if(count($op_data) > 0) {
					$r .= implode(" ", $op_data);
					$have_where = true;
				}
			}
		}
	}
	
	if(!$have_where) $r .= " 1 ";
	
	if(count($q['group']) > 0) {
		$r .= " group by " . implode(", ", $q['group']);
	}
	
	if(trim($q['limit']) != "") {
		$r .= " limit " . $q['limit'];
	}
	
	return $r;
}

function strip_html_tags( $text ) {
    $text = preg_replace(
        array(
          // Remove invisible content
            '@<head[^>]*?>.*?</head>@siu',
            '@<style[^>]*?>.*?</style>@siu',
            '@<script[^>]*?.*?</script>@siu',
            '@<object[^>]*?.*?</object>@siu',
            '@<embed[^>]*?.*?</embed>@siu',
            '@<applet[^>]*?.*?</applet>@siu',
            '@<noframes[^>]*?.*?</noframes>@siu',
            '@<noscript[^>]*?.*?</noscript>@siu',
            '@<noembed[^>]*?.*?</noembed>@siu',
          // Add line breaks before and after blocks
            '@</?((address)|(blockquote)|(center)|(del))@iu',
            '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
            '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
            '@</?((table)|(th)|(td)|(caption))@iu',
            '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
            '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
            '@</?((frameset)|(frame)|(iframe))@iu',
        ),
        array(
            ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',"$0", "$0", "$0", "$0", "$0", "$0","$0", "$0",), $text );
  
    // you can exclude some html tags here, in this case B and A tags       
    return strip_tags( $text , '' );
}

?>