refactor this: $in_title_and_content = " AND REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(entriesxw.title || ' ' || entriesxw.content, '<em>', ''), '</em>', ''), '</strong>', ''), '</u>', ''), '<strong>', ''), '</u>', ''), '<br />', ''), '</li>', ''), '<li>', ''), '</p>', ''), '<p>', ''), '<span class=" . $securityName . ">', ''), '</span>', '')";


$in_title = " AND REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(entriesxw.title, '<em>', ''), '</em>', ''), '</strong>', ''), '</u>', ''), '<strong>', ''), '</u>', ''), '<br />', ''), '</li>', ''), '<li>', ''), '</p>', ''), '<p>', ''), '<span class=" . $securityName . ">', ''), '</span>', '')";

	if ($postedData["show"]=='2') {
		
$sql= "SELECT DISTINCT  entriesxw.id, entriesxw.updated at time zone 'utc' at time zone 'EST5EDT' as updateddate, entriesxw.date_entered at time zone 'utc' at time zone 'EST5EDT' as date_entereddate, entriesxw.title, entriesxw.content, entriesxw.link
FROM entriesxw
WHERE 1=1";
if ((isset($postedData["startdate"]) && !empty($postedData["startdate"])) && (isset($postedData["stopdate"]) && !empty($postedData["stopdate"]))) {
	$sql = $sql . " AND entriesxw.updated_usa_date between '" . pg_escape_string($postedData["startdate"]) . "' AND '" . pg_escape_string($postedData["stopdate"]) . "'";	
}
if (isset($postedData["dater"]) && !empty($postedData["dater"])) {
	$sql = $sql . " AND entriesxw.updated_usa_date = '" . pg_escape_string($postedData["dater"]) . "'";
}	
if (isset($postedData["aterm"]) && !empty($postedData["aterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["aterm"]) . "%'";
}	
if (isset($postedData["bterm"]) && !empty($postedData["bterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["bterm"]) . "%'";
}
if (isset($postedData["cterm"]) && !empty($postedData["cterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["cterm"]) . "%'";
}
if (isset($postedData["dterm"]) && !empty($postedData["dterm"])) {
	$sql .= $in_title_and_content . " NOT ILIKE '%" . pg_escape_string($postedData["dterm"]) . "%'";
}
if (isset($postedData["eterm"]) && !empty($postedData["eterm"])) {
	$sql .= $in_title_and_content . " NOT ILIKE '%" . pg_escape_string($postedData["eterm"]) . "%'";
}


$sql = $sql . " AND entriesxw.link NOT ILIKE '%https://www.sec.gov/Archives/%' ORDER BY updateddate DESC
LIMIT 500;";
} 
elseif ($postedData["show"]=='3') {
		
$sql= "SELECT DISTINCT  entriesxw.id, entriesxw.updated at time zone 'utc' at time zone 'EST5EDT' as updateddate, entriesxw.date_entered at time zone 'utc' at time zone 'EST5EDT' as date_entereddate, entriesxw.title, entriesxw.content, entriesxw.link
FROM entriesxw
WHERE 1=1";

if ((isset($postedData["startdate"]) && !empty($postedData["startdate"])) && (isset($postedData["stopdate"]) && !empty($postedData["stopdate"]))) {
	$sql = $sql . " AND entriesxw.updated_usa_date between '" . pg_escape_string($postedData["startdate"]) . "' AND '" . pg_escape_string($postedData["stopdate"]) . "'";	
}
if (isset($postedData["dater"]) && !empty($postedData["dater"])) {
	$sql = $sql . " AND entriesxw.updated_usa_date = '" . pg_escape_string($postedData["dater"]) . "'";
}	
if (isset($postedData["aterm"]) && !empty($postedData["aterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["aterm"]) . "%'";
}	
if (isset($postedData["bterm"]) && !empty($postedData["bterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["bterm"]) . "%'";
}
if (isset($postedData["cterm"]) && !empty($postedData["cterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["cterm"]) . "%'";
}
if (isset($postedData["dterm"]) && !empty($postedData["dterm"])) {
	$sql .= $in_title_and_content . " NOT ILIKE '%" . pg_escape_string($postedData["dterm"]) . "%'";
}
if (isset($postedData["eterm"]) && !empty($postedData["eterm"])) {
	$sql .= $in_title_and_content . " NOT ILIKE '%" . pg_escape_string($postedData["eterm"]) . "%'";
}


$sql = $sql . " AND entriesxw.link ILIKE '%https://www.sec.gov/Archives/%' ORDER BY updateddate DESC
LIMIT 500;";
} 
elseif ($postedData["show"]=='4') {
		
$sql= "SELECT DISTINCT  entriesxw.id, entriesxw.updated at time zone 'utc' at time zone 'EST5EDT' as updateddate, entriesxw.date_entered at time zone 'utc' at time zone 'EST5EDT' as date_entereddate, entriesxw.title, entriesxw.content, entriesxw.link
FROM entriesxw
WHERE 1=1";

if ((isset($postedData["startdate"]) && !empty($postedData["startdate"])) && (isset($postedData["stopdate"]) && !empty($postedData["stopdate"]))) {
	$sql = $sql . " AND entriesxw.updated_usa_date between '" . pg_escape_string($postedData["startdate"]) . "' AND '" . pg_escape_string($postedData["stopdate"]) . "'";	
}
if (isset($postedData["dater"]) && !empty($postedData["dater"])) {
	$sql = $sql . " AND entriesxw.updated_usa_date = '" . pg_escape_string($postedData["dater"]) . "'";
}	
if (isset($postedData["aterm"]) && !empty($postedData["aterm"])) {
	$sql .= $in_title . " ILIKE '%" . pg_escape_string($postedData["aterm"]) . "%'";
}	
if (isset($postedData["bterm"]) && !empty($postedData["bterm"])) {
	$sql .= $in_title . " ILIKE '%" . pg_escape_string($postedData["bterm"]) . "%'";
}
if (isset($postedData["cterm"]) && !empty($postedData["cterm"])) {
	$sql .= $in_title . " ILIKE '%" . pg_escape_string($postedData["cterm"]) . "%'";
}
if (isset($postedData["dterm"]) && !empty($postedData["dterm"])) {
	$sql .= $in_title . " NOT ILIKE '%" . pg_escape_string($postedData["dterm"]) . "%'";
}
if (isset($postedData["eterm"]) && !empty($postedData["eterm"])) {
	$sql .= $in_title . " NOT ILIKE '%" . pg_escape_string($postedData["eterm"]) . "%'";
}


$sql = $sql . " AND entriesxw.link NOT ILIKE '%https://www.sec.gov/Archives/%' ORDER BY updateddate DESC
LIMIT 500;";
}

else {
	
$sql= "SELECT DISTINCT entriesxw.id, entriesxw.updated at time zone 'utc' at time zone 'EST5EDT' as updateddate, entriesxw.date_entered at time zone 'utc' at time zone 'EST5EDT' as date_entereddate, entriesxw.title, entriesxw.content, entriesxw.link
FROM entriesxw
WHERE entriesxw.updated_usa_date > now()::date - 4";
	
if (isset($postedData["aterm"]) && !empty($postedData["aterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["aterm"]) . "%'";
}	
if (isset($postedData["bterm"]) && !empty($postedData["bterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["bterm"]) . "%'";
}
if (isset($postedData["cterm"]) && !empty($postedData["cterm"])) {
	$sql .= $in_title_and_content . " ILIKE '%" . pg_escape_string($postedData["cterm"]) . "%'";
}
if (isset($postedData["dterm"]) && !empty($postedData["dterm"])) {
	$sql .= $in_title_and_content . " NOT ILIKE '%" . pg_escape_string($postedData["dterm"]) . "%'";
}
if (isset($postedData["eterm"]) && !empty($postedData["eterm"])) {
	$sql .= $in_title_and_content . " NOT ILIKE '%" . pg_escape_string($postedData["eterm"]) . "%'";
}

$sql .= " AND entriesxw.link NOT ILIKE '%https://www.fg.com/Archives/%' ORDER BY updateddate DESC LIMIT 500;";
//echo $sql;
}