Created
October 12, 2024 15:47
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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