function dbem_replace_placeholders($format, $event, $target = "html")
{
    $event_string = $format;
    preg_match_all("/#@?_?[A-Za-z0-9]+/", $format, $placeholders);
    foreach ($placeholders[0] as $result) {
        // echo "RESULT: $result <br>";
        // matches alla fields placeholder
        //TODO CUSTOM FIX FOR Brian
        // EVENTUALLY REMOVE
        if (preg_match('/#_JCCSTARTTIME/', $result)) {
            $time = substr($event['event_start_time'], 0, 5);
            $event_string = str_replace($result, $time, $event_string);
        }
        // END of REMOVE
        if (preg_match('/#_24HSTARTTIME/', $result)) {
            $time = substr($event['event_start_time'], 0, 5);
            $event_string = str_replace($result, $time, $event_string);
        }
        if (preg_match('/#_24HENDTIME/', $result)) {
            $time = substr($event['event_end_time'], 0, 5);
            $event_string = str_replace($result, $time, $event_string);
        }
        if (preg_match('/#_12HSTARTTIME/', $result)) {
            $AMorPM = "AM";
            $hour = substr($event['event_start_time'], 0, 2);
            $minute = substr($event['event_start_time'], 3, 2);
            if ($hour > 12) {
                $hour = $hour - 12;
                $AMorPM = "PM";
            }
            $time = "{$hour}:{$minute} {$AMorPM}";
            $event_string = str_replace($result, $time, $event_string);
        }
        if (preg_match('/#_12HENDTIME/', $result)) {
            $AMorPM = "AM";
            $hour = substr($event['event_end_time'], 0, 2);
            $minute = substr($event['event_end_time'], 3, 2);
            if ($hour > 12) {
                $hour = $hour - 12;
                $AMorPM = "PM";
            }
            $time = "{$hour}:{$minute} {$AMorPM}";
            $event_string = str_replace($result, $time, $event_string);
        }
        if (preg_match('/#_MAP/', $result)) {
            $location = dbem_get_location($event['location_id']);
            $map_div = dbem_single_location_map($location);
            $event_string = str_replace($result, $map_div, $event_string);
        }
        if (preg_match('/#_ADDBOOKINGFORM/', $result)) {
            $rsvp_is_active = get_option('dbem_gmap_is_active');
            if ($event['event_rsvp']) {
                $rsvp_add_module .= dbem_add_booking_form();
            } else {
                $rsvp_add_module .= "";
            }
            $event_string = str_replace($result, $rsvp_add_module, $event_string);
        }
        if (preg_match('/#_REMOVEBOOKINGFORM/', $result)) {
            $rsvp_is_active = get_option('dbem_gmap_is_active');
            if ($event['event_rsvp']) {
                $rsvp_delete_module .= dbem_delete_booking_form();
            } else {
                $rsvp_delete_module .= "";
            }
            $event_string = str_replace($result, $rsvp_delete_module, $event_string);
        }
        if (preg_match('/#_AVAILABLESEATS/', $result)) {
            $rsvp_is_active = get_option('dbem_gmap_is_active');
            if ($event['event_rsvp']) {
                $availble_seats .= dbem_get_available_seats($event['event_id']);
            } else {
                $availble_seats .= "";
            }
            $event_string = str_replace($result, $availble_seats, $event_string);
        }
        if (preg_match('/#_LINKEDNAME/', $result)) {
            $events_page_id = get_option('dbem_events_page');
            $event_page_link = get_permalink($events_page_id);
            if (stristr($event_page_link, "?")) {
                $joiner = "&amp;";
            } else {
                $joiner = "?";
            }
            $event_string = str_replace($result, "<a href='" . get_permalink($events_page_id) . $joiner . "event_id=" . $event['event_id'] . "'   title='" . $event['event_name'] . "'>" . $event['event_name'] . "</a>", $event_string);
        }
        if (preg_match('/#_EVENTPAGEURL/', $result)) {
            $events_page_id = get_option('dbem_events_page');
            $event_page_link = get_permalink($events_page_id);
            if (stristr($event_page_link, "?")) {
                $joiner = "&amp;";
            } else {
                $joiner = "?";
            }
            $event_string = str_replace($result, get_permalink($events_page_id) . $joiner . "event_id=" . $event['event_id'], $event_string);
        }
        if (preg_match('/#_(NAME|NOTES|SEATS)/', $result)) {
            $field = "event_" . ltrim(strtolower($result), "#_");
            $field_value = $event[$field];
            if ($field == "event_notes") {
                if ($target == "html") {
                    $field_value = apply_filters('dbem_notes', $field_value);
                } else {
                    if ($target == "map") {
                        $field_value = apply_filters('dbem_notes_map', $field_value);
                    } else {
                        $field_value = apply_filters('dbem_notes_rss', $field_value);
                    }
                }
            } else {
                if ($target == "html") {
                    $field_value = apply_filters('dbem_general', $field_value);
                } else {
                    $field_value = apply_filters('dbem_general_rss', $field_value);
                }
            }
            $event_string = str_replace($result, $field_value, $event_string);
        }
        if (preg_match('/#_(ADDRESS|TOWN|PROVINCE)/', $result)) {
            $field = "location_" . ltrim(strtolower($result), "#_");
            $field_value = $event[$field];
            if ($field == "event_notes") {
                if ($target == "html") {
                    $field_value = apply_filters('dbem_notes', $field_value);
                } else {
                    if ($target == "map") {
                        $field_value = apply_filters('dbem_notes_map', $field_value);
                    } else {
                        $field_value = apply_filters('dbem_notes_rss', $field_value);
                    }
                }
            } else {
                if ($target == "html") {
                    $field_value = apply_filters('dbem_general', $field_value);
                } else {
                    $field_value = apply_filters('dbem_general_rss', $field_value);
                }
            }
            $event_string = str_replace($result, $field_value, $event_string);
        }
        if (preg_match('/#_(LOCATION)$/', $result)) {
            $field = "location_name";
            $field_value = $event[$field];
            if ($target == "html") {
                $field_value = apply_filters('dbem_general', $field_value);
            } else {
                $field_value = apply_filters('dbem_general_rss', $field_value);
            }
            $event_string = str_replace($result, $field_value, $event_string);
        }
        if (preg_match('/#_CONTACTNAME$/', $result)) {
            $event['event_contactperson_id'] ? $user_id = $event['event_contactperson_id'] : ($user_id = get_option('dbem_default_contact_person'));
            $name = dbem_get_user_name($user_id);
            $event_string = str_replace($result, $name, $event_string);
        }
        if (preg_match('/#_CONTACTEMAIL$/', $result)) {
            $event['event_contactperson_id'] ? $user_id = $event['event_contactperson_id'] : ($user_id = get_option('dbem_default_contact_person'));
            $email = dbem_get_user_email($user_id);
            $event_string = str_replace($result, dbem_ascii_encode($email), $event_string);
        }
        if (preg_match('/#_CONTACTPHONE$/', $result)) {
            $event['event_contactperson_id'] ? $user_id = $event['event_contactperson_id'] : ($user_id = get_option('dbem_default_contact_person'));
            $phone = dbem_get_user_phone($user_id);
            $event_string = str_replace($result, dbem_ascii_encode($phone), $event_string);
        }
        if (preg_match('/#_(IMAGE)/', $result)) {
            if ($event['location_image_url'] != '') {
                $location_image = "<img src='" . $event['location_image_url'] . "' alt='" . $event['location_name'] . "'/>";
            } else {
                $location_image = "";
            }
            $event_string = str_replace($result, $location_image, $event_string);
        }
        if (preg_match('/#_(LOCATIONPAGEURL)/', $result)) {
            $events_page_link = dbem_get_events_page(true, false);
            if (stristr($events_page_link, "?")) {
                $joiner = "&amp;";
            } else {
                $joiner = "?";
            }
            $venue_page_link = $events_page_link . $joiner . "location_id=" . $event['location_id'];
            $event_string = str_replace($result, $venue_page_link, $event_string);
        }
        // matches all PHP time placeholders for endtime
        if (preg_match('/^#@[dDjlNSwzWFmMntLoYy]$/', $result)) {
            $event_string = str_replace($result, mysql2date(ltrim($result, "#@"), $event['event_end_date']), $event_string);
        }
        // matches all PHP date placeholders
        if (preg_match('/^#[dDjlNSwzWFmMntLoYy]$/', $result)) {
            // echo "-inizio-";
            $event_string = str_replace($result, mysql2date(ltrim($result, "#"), $event['event_start_date']), $event_string);
            // echo $event_string;
        }
        // matches all PHP time placeholders
        if (preg_match('/^#@[aABgGhHisueIOPTZcrU]$/', $result)) {
            $event_string = str_replace($result, mysql2date(ltrim($result, "#@"), "0000-00-00 " . $event['event_end_time']), $event_string);
            // echo $event_string;
        }
        if (preg_match('/^#[aABgGhHisueIOPTZcrU]$/', $result)) {
            $event_string = str_replace($result, mysql2date(ltrim($result, "#"), "0000-00-00 " . $event['event_start_time']), $event_string);
            // echo $event_string;
        }
        /* Marcus/ROR Begin Edit*/
        //Add a placeholder for categories
        if (preg_match('/#_CATEGORY$/', $result)) {
            $category = dbem_get_event_category($event['event_id']);
            if (stristr($event_page_link, "?")) {
                $joiner = "&amp;";
            } else {
                $joiner = "?";
            }
            $event_string = str_replace($result, "<a href='" . get_permalink($events_page_id) . $joiner . "category=" . $category['category_id'] . "'   title='" . $category['category_name'] . "'>" . $category['category_name'] . "</a>", $event_string);
        }
        /* Marcus/ROR End Edit */
    }
    /* Marcus Begin Edit */
    preg_match_all("/#@?_\\{[A-Za-z0-9 -\\/,\\.\\\\]+\\}/", $format, $placeholders);
    foreach ($placeholders[0] as $result) {
        if (substr($result, 0, 3) == "#@_") {
            $date = 'event_end_date';
            $offset = 4;
        } else {
            $date = 'event_start_date';
            $offset = 3;
        }
        $event_string = str_replace($result, mysql2date(substr($result, $offset, strlen($result) - ($offset + 1)), $event[$date]), $event_string);
    }
    /* Marcus End Edit */
    return $event_string;
}
function dbem_replace_placeholders($format, $event, $target = "html")
{
    $event_string = $format;
    preg_match_all("/#@?_?[A-Za-z0-9]+/", $format, $placeholders);
    foreach ($placeholders[0] as $result) {
        // echo "RESULT: $result <br>";
        // matches alla fields placeholder
        //TODO CUSTOM FIX FOR Brian
        // EVENTUALLY REMOVE
        if (preg_match('/#_JCCSTARTTIME/', $result)) {
            $time = substr($event['event_start_time'], 0, 5);
            $event_string = str_replace($result, $time, $event_string);
        }
        // END of REMOVE
        if (preg_match('/#_24HSTARTTIME/', $result)) {
            $time = substr($event['event_start_time'], 0, 5);
            $event_string = str_replace($result, $time, $event_string);
        }
        if (preg_match('/#_24HENDTIME/', $result)) {
            $time = substr($event['event_end_time'], 0, 5);
            $event_string = str_replace($result, $time, $event_string);
        }
        if (preg_match('/#_12HSTARTTIME/', $result)) {
            $AMorPM = "AM";
            $hour = substr($event['event_start_time'], 0, 2);
            $minute = substr($event['event_start_time'], 3, 2);
            if ($hour > 12) {
                $hour = $hour - 12;
                $AMorPM = "PM";
            }
            $time = "{$hour}:{$minute} {$AMorPM}";
            $event_string = str_replace($result, $time, $event_string);
        }
        if (preg_match('/#_12HENDTIME/', $result)) {
            $AMorPM = "AM";
            $hour = substr($event['event_end_time'], 0, 2);
            $minute = substr($event['event_end_time'], 3, 2);
            if ($hour > 12) {
                $hour = $hour - 12;
                $AMorPM = "PM";
            }
            $time = "{$hour}:{$minute} {$AMorPM}";
            $event_string = str_replace($result, $time, $event_string);
        }
        if (preg_match('/#_MAP/', $result)) {
            $location = dbem_get_location($event['location_id']);
            $map_div = dbem_single_location_map($location);
            $event_string = str_replace($result, $map_div, $event_string);
        }
        if (preg_match('/#_ADDBOOKINGFORM/', $result)) {
            $rsvp_is_active = get_option('dbem_gmap_is_active');
            if ($event['event_rsvp']) {
                $rsvp_add_module .= dbem_add_booking_form();
            } else {
                $rsvp_add_module .= "";
            }
            $event_string = str_replace($result, $rsvp_add_module, $event_string);
        }
        if (preg_match('/#_REMOVEBOOKINGFORM/', $result)) {
            $rsvp_is_active = get_option('dbem_gmap_is_active');
            if ($event['event_rsvp']) {
                $rsvp_delete_module .= dbem_delete_booking_form();
            } else {
                $rsvp_delete_module .= "";
            }
            $event_string = str_replace($result, $rsvp_delete_module, $event_string);
        }
        if (preg_match('/#_AVAILABLESEATS/', $result)) {
            $rsvp_is_active = get_option('dbem_gmap_is_active');
            if ($event['event_rsvp']) {
                $availble_seats .= dbem_get_available_seats($event['event_id']);
            } else {
                $availble_seats .= "";
            }
            $event_string = str_replace($result, $availble_seats, $event_string);
        }
        if (preg_match('/#_LINKEDNAME/', $result)) {
            $events_page_id = get_option('dbem_events_page');
            $event_page_link = get_permalink($events_page_id);
            if (stristr($event_page_link, "?")) {
                $joiner = "&amp;";
            } else {
                $joiner = "?";
            }
            $event_string = str_replace($result, "<a href='" . get_permalink($events_page_id) . $joiner . "event_id=" . $event['event_id'] . "'   title='" . $event['event_name'] . "'>" . $event['event_name'] . "</a>", $event_string);
        }
        if (preg_match('/#_EVENTPAGEURL(\\[(.+\\)]))?/', $result)) {
            $events_page_id = get_option('dbem_events_page');
            if (stristr($event_page_link, "?")) {
                $joiner = "&amp;";
            } else {
                $joiner = "?";
            }
            $event_string = str_replace($result, get_permalink($events_page_id) . $joiner . "event_id=" . $event['event_id'], $event_string);
        }
        if (preg_match('/#_(NAME|NOTES|SEATS|EXCERPT)/', $result)) {
            $field = "event_" . ltrim(strtolower($result), "#_");
            $field_value = $event[$field];
            if ($field == "event_notes" || $field == "event_excerpt") {
                /* Marcus Begin Edit */
                if ($target == "html") {
                    //If excerpt, we use more link text
                    if ($field == "event_excerpt") {
                        $matches = explode('<!--more-->', $event['event_notes']);
                        $field_value = $matches[0];
                        $field_value = apply_filters('dbem_notes_excerpt', $field_value);
                    } else {
                        $field_value = apply_filters('dbem_notes', $field_value);
                    }
                    //$field_value = apply_filters('the_content', $field_value); - chucks a wobbly if we do this.
                } else {
                    if ($target == "map") {
                        $field_value = apply_filters('dbem_notes_map', $field_value);
                    } else {
                        if ($field == "event_excerpt") {
                            $matches = explode('<!--more-->', $event['event_notes']);
                            $field_value = htmlentities($matches[0]);
                            $field_value = apply_filters('dbem_notes_rss', $field_value);
                        } else {
                            $field_value = apply_filters('dbem_notes_rss', $field_value);
                        }
                        $field_value = apply_filters('the_content_rss', $field_value);
                    }
                }
                /* Marcus End Edit */
            } else {
                if ($target == "html") {
                    $field_value = apply_filters('dbem_general', $field_value);
                } else {
                    $field_value = apply_filters('dbem_general_rss', $field_value);
                }
            }
            $event_string = str_replace($result, $field_value, $event_string);
        }
        if (preg_match('/#_(ADDRESS|TOWN|PROVINCE)/', $result)) {
            $field = "location_" . ltrim(strtolower($result), "#_");
            $field_value = $event[$field];
            if ($field == "event_notes") {
                if ($target == "html") {
                    $field_value = apply_filters('dbem_notes', $field_value);
                } else {
                    if ($target == "map") {
                        $field_value = apply_filters('dbem_notes_map', $field_value);
                    } else {
                        $field_value = apply_filters('dbem_notes_rss', $field_value);
                    }
                }
            } else {
                if ($target == "html") {
                    $field_value = apply_filters('dbem_general', $field_value);
                } else {
                    $field_value = apply_filters('dbem_general_rss', $field_value);
                }
            }
            $event_string = str_replace($result, $field_value, $event_string);
        }
        if (preg_match('/#_(LOCATION)$/', $result)) {
            $field = "location_name";
            $field_value = $event[$field];
            if ($target == "html") {
                $field_value = apply_filters('dbem_general', $field_value);
            } else {
                $field_value = apply_filters('dbem_general_rss', $field_value);
            }
            $event_string = str_replace($result, $field_value, $event_string);
        }
        if (preg_match('/#_CONTACTNAME$/', $result)) {
            $event['event_contactperson_id'] ? $user_id = $event['event_contactperson_id'] : ($user_id = get_option('dbem_default_contact_person'));
            $name = dbem_get_user_name($user_id);
            $event_string = str_replace($result, $name, $event_string);
        }
        if (preg_match('/#_CONTACTEMAIL$/', $result)) {
            $event['event_contactperson_id'] ? $user_id = $event['event_contactperson_id'] : ($user_id = get_option('dbem_default_contact_person'));
            $email = dbem_get_user_email($user_id);
            $event_string = str_replace($result, dbem_ascii_encode($email), $event_string);
        }
        if (preg_match('/#_CONTACTPHONE$/', $result)) {
            $event['event_contactperson_id'] ? $user_id = $event['event_contactperson_id'] : ($user_id = get_option('dbem_default_contact_person'));
            $phone = dbem_get_user_phone($user_id);
            $event_string = str_replace($result, dbem_ascii_encode($phone), $event_string);
        }
        if (preg_match('/#_(IMAGE)/', $result)) {
            if ($event['location_image_url'] != '') {
                $location_image = "<img src='" . $event['location_image_url'] . "' alt='" . $event['location_name'] . "'/>";
            } else {
                $location_image = "";
            }
            $event_string = str_replace($result, $location_image, $event_string);
        }
        if (preg_match('/#_(LOCATIONPAGEURL)/', $result)) {
            $events_page_link = dbem_get_events_page(true, false);
            if (stristr($events_page_link, "?")) {
                $joiner = "&amp;";
            } else {
                $joiner = "?";
            }
            $venue_page_link = $events_page_link . $joiner . "location_id=" . $event['location_id'];
            $event_string = str_replace($result, $venue_page_link, $event_string);
        }
        // matches all PHP time placeholders for endtime
        if (preg_match('/^#@[dDjlNSwzWFmMntLoYy]$/', $result)) {
            $event_string = str_replace($result, mysql2date(ltrim($result, "#@"), $event['event_end_date']), $event_string);
        }
        // matches all PHP date placeholders
        if (preg_match('/^#[dDjlNSwzWFmMntLoYy]$/', $result)) {
            // echo "-inizio-";
            $event_string = str_replace($result, mysql2date(ltrim($result, "#"), $event['event_start_date']), $event_string);
            // echo $event_string;
        }
        // matches all PHP time placeholders
        if (preg_match('/^#@[aABgGhHisueIOPTZcrU]$/', $result)) {
            $event_string = str_replace($result, mysql2date(ltrim($result, "#@"), "2000-10-10 " . $event['event_end_time']), $event_string);
            // echo $event_string;
        }
        if (preg_match('/^#[aABgGhHisueIOPTZcrU]$/', $result)) {
            $event_string = str_replace($result, mysql2date(ltrim($result, "#"), "2000-10-10 " . $event['event_start_time']), $event_string);
            //echo $event['event_start_time'];
            //echo mysql2date('h:i A', '2010-10-10 23:35:00')."<br/>";
            // echo $event_string;
        }
        /* Marcus Begin Edit*/
        //Add a placeholder for categories
        if (preg_match('/^#_CATEGORY$/', $result)) {
            $category = dbem_get_event_category($event['event_id']);
            $event_string = str_replace($result, $category['category_name'], $event_string);
        }
        /* Marcus End Edit */
    }
    /* Marcus Begin Edit */
    preg_match_all("/#@?_\\{[A-Za-z0-9 -\\/,\\.\\\\]+\\}/", $format, $results);
    foreach ($results[0] as $result) {
        if (substr($result, 0, 3) == "#@_") {
            $date = 'event_end_date';
            $offset = 4;
        } else {
            $date = 'event_start_date';
            $offset = 3;
        }
        if ($date == 'event_end_date' && $event[$date] == $event['event_start_date']) {
            $event_string = str_replace($result, '', $event_string);
        } else {
            $event_string = str_replace($result, mysql2date(substr($result, $offset, strlen($result) - ($offset + 1)), $event[$date]), $event_string);
        }
    }
    /* Marcus End Edit */
    /* Marcus Begin Edit */
    //This is for the custom attributes
    preg_match_all("/#_ATT\\{.+?\\}(\\{.+?\\})?/", $format, $results);
    foreach ($results[0] as $resultKey => $result) {
        //Strip string of placeholder and just leave the reference
        $attRef = substr(substr($result, 0, strpos($result, '}')), 6);
        $attString = $event['event_attributes'][$attRef];
        if (trim($attString) == '' && $results[1][$resultKey] != '') {
            //Check to see if we have a second set of braces;
            $attString = substr($results[1][$resultKey], 1, strlen(trim($results[1][$resultKey])) - 2);
        }
        $event_string = str_replace($result, $attString, $event_string);
    }
    /* Marcus End Edit */
    return $event_string;
}