Skip to content

Instantly share code, notes, and snippets.

@stevebauman
Created October 12, 2024 15:47
Show Gist options
  • Save stevebauman/2ae0da3a0189d41066bab5c5a7425a21 to your computer and use it in GitHub Desktop.
Save stevebauman/2ae0da3a0189d41066bab5c5a7425a21 to your computer and use it in GitHub Desktop.
Outlook Safe Links URL Replacer in Laravel (replace safe link URLs with their true value)
<?php
namespace App\Support;
use DOMXPath;
use DOMDocument;
class SafeLinksReplacer
{
public static function replace(string $html): string
{
$doc = new DOMDocument('1.0', 'UTF-8');
// Suppress errors due to malformed HTML.
libxml_use_internal_errors(true);
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($doc);
libxml_clear_errors();
foreach ($xpath->query('//a[@originalsrc]') as $link) {
$link->setAttribute('href', $link->getAttribute('originalsrc'));
$link->removeAttribute('originalsrc');
}
return $doc->saveHTML();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment