There are different ways to achieve this.
You can point all the redirects to the same page with the safe URL as a GET
parameter (remember to use urlencode):
<a href="/handler.php?safeurl=www.google.it%2Ftest" target="_blank">Link</a>
Then in handler.php
something like:
$desturl = isset($_GET["safeurl"]) ? $_GET["safeurl"] : false;
if($desturl != false){
//do something
header("location: ".$desturl);
}
If you want to use data-saferedirecturl
you have to use some JavaScript, and intercat somehow with your server (ex. ajax calls).
Example (using jQuery):
HTML:
<a href="xxxxx" class="test" target="_blank" data-saferedirecturl="https://www.mywebsite.com/url?hl=en&q=xxxx">Link</a>
JavaScript:
$(".test").click(function(e){
e.preventDefault();
let url = $(this).data("saferedirecturl");
// do ajax or check somehow the URL
location.href = url;
})