How can PHP code be optimized to efficiently handle both internal and external links in a news display?

To efficiently handle both internal and external links in a news display, we can use PHP to check if a link is internal or external and then format it accordingly. One way to do this is by checking the domain of the link and comparing it to the current domain. If the domains match, it's an internal link, and if they don't match, it's an external link.

function format_link($link) {
    $current_domain = $_SERVER['HTTP_HOST'];
    $link_domain = parse_url($link, PHP_URL_HOST);

    if ($current_domain == $link_domain) {
        return "<a href='$link'>Internal Link</a>";
    } else {
        return "<a href='$link' target='_blank'>External Link</a>";
    }
}

// Example of how to use the function
$internal_link = "http://example.com/internal";
$external_link = "http://external.com";
echo format_link($internal_link); // Output: <a href='http://example.com/internal'>Internal Link</a>
echo format_link($external_link); // Output: <a href='http://external.com' target='_blank'>External Link</a>