How can external links be filtered out when creating a sitemap using PHP?

When creating a sitemap using PHP, you can filter out external links by checking if the URL starts with "http" or "https". If the URL does not start with your domain, you can exclude it from the sitemap.

// Sample code to filter out external links when creating a sitemap
$domain = 'example.com';

// Loop through your list of URLs and filter out external links
foreach ($urls as $url) {
    if (strpos($url, $domain) !== false) {
        // Add the URL to the sitemap
        echo '<url><loc>' . $url . '</loc></url>';
    }
}